J'essaie de créer un bouton en utilisant un div. Ce bouton div appellera un événement linklicklick. pour une raison quelconque, il ne fonctionne pas en chrome. Cela ne déclenche pas l'événement de bouton dans Chrome? Mais il fonctionne très bien dans d'autres navigateurs ..Javascript ne fonctionne pas dans Chrome? mais dans FF et IE fonctionne
Voici mon code ..
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
ValidateRequest="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="dvButton" runat="server" ></div>
<asp:LinkButton ID="DummyBtn" runat="server" Text="Dummy" OnClick="DummyBtn_Click"></asp:LinkButton>
<input type="hidden" id="hdnConfigId" runat="server" />
<input type="hidden" id="hdnSelected" runat="server" />
</form>
</body>
<script language="javascript" type="text/javascript">
function Selected(value, selected) {
document.getElementById('<%=hdnConfigId.ClientID %>').value = value;
document.getElementById('<%=hdnSelected.ClientID %>').value = selected;
document.getElementById('<%=DummyBtn.ClientID %>').click(); //This does not work in Chrome
}
</script>
</html>
code derrière:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
dvButton.InnerHtml = "<a href=\"javascript:void(0);\" id=1234 onclick=\"javascript:Selected('1234','Test');return false;\" >TEST</a>";
}
protected void DummyBtn_Click(object sender, EventArgs e)
{
string id = hdnConfigId.Value;
string selected = hdnSelected.Value;
//Logic to retrieve data by passing these parameters into DB
}
}
}
SOURCE RENDUE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form name="form1" method="post" action="WebForm1.aspx" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUHNjcxMjAzMQ9kFgICAQ9kFgICAQ8WAh4JaW5uZXJodG1sBWo8YSBocmVmPSJqYXZhc2NyaXB0OnZvaWQoMCk7IiBpZD0xMjM0IG9uY2xpY2s9ImphdmFzY3JpcHQ6U2VsZWN0ZWQoJzEyMzQnLCdUZXN0Jyk7cmV0dXJuIGZhbHNlOyIgPlRFU1Q8L2E+ZGSqT177YHIKIjfz5j0oOl9lGktifA==" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<div>
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBALUn5j+AQKEzpmBDwKf7rOjAgLVqJ/GB0fyISUyIuzPcZ4CYvSLltNpE/oA" />
</div>
<div id="dvButton"><a href="javascript:void(0);" id=1234 onclick="javascript:Selected('1234','Test');return false;" >TEST</a></div>
<a id="DummyBtn" href="javascript:__doPostBack('DummyBtn','')">Dummy</a>
<input name="hdnConfigId" type="hidden" id="hdnConfigId" value="1234" />
<input name="hdnSelected" type="hidden" id="hdnSelected" value="Test" />
</form>
</body>
<script language="javascript" type="text/javascript">
function Selected(value, selected) {
document.getElementById('hdnConfigId').value = value;
document.getElementById('hdnSelected').value = selected;
document.getElementById('DummyBtn').click();
}
</script>
</html>
Je vois un espace après chaque .ClientID, essayez de supprimer l'espace et laissez-nous savoir si cela fixe ou pas votre problème – ComputerSaysNo
Pouvons-nous voir la source rendue? –
@Dorin Cela ne résoudra pas son problème. L'espace est côté serveur, donc ne provoquera pas d'espace côté client. –