![](/img/trans.png)
[英]HiddenField EventHandler ValueChanged not fired when changed via Javascript
[英]HiddenField.ValueChanged Event not firing when changed from Javascript
更新我改為將Javascript移動到ASPX站點,並為其添加了回發功能。 現在它有效。 感謝@orgtigger特別是@lucidgold花時間幫助我!
這是有效的更新代碼!
<script type="text/javascript">
function changevalue(katoid) {
$('#<%=txtboxchosenkat.ClientID%>').val(katoid);
__doPostBack('<%= updpnlgridview.ClientID %>', '');
}
</script>
碼:
<asp:UpdatePanel ID="updpnlgridview" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="txtboxchosenkat" style="display:none;" runat="server" OnTextChanged="txtboxchosenkat_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:GridView ID="gridview" runat="server"></asp:GridView>
</ContentTemplate>
</asp:UpdatePane
代碼隱藏:
protected void hidfldchosenkat_ValueChanged(object sender, EventArgs e)
{
SqlConnection cn2 = new SqlConnection("Server=**,***,***,**;Database=******;
User Id=******;Password=******;");
SqlCommand cmd2 = new SqlCommand("SELECT * FROM tblProducts where KatID='" +
txtboxchosenkat.Text + "'", cn2);
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
da2.SelectCommand.CommandText = cmd2.CommandText.ToString();
DataTable dt = new DataTable();
da2.Fill(dt);
gridview.DataSource = dt.DefaultView;
gridview.DataBind();
}
鏈接(只是構成鏈接的代碼的一部分):
string line = String.Format("<li><a href='#' onclick='changevalue(" + pid + ");'>{0}",
menuText + "</a>");
舊帖子我需要根據HiddenField的值更新GridView。 我目前正在使用一個按鈕來填充GridView,但是一旦HiddenField中的值發生變化,我想立即自動完成。
但是當我用javascript更改值時,事件不會觸發。
(在TextBox
及其OnTextChanged
事件的情況下也會發生同樣的事情。)
不確定這是否意味着它的工作方式。
隱藏字段不會產生回發(沒有AutoPostBack屬性),這意味着當隱藏字段的值發生更改時不會發生回發。 但是,當頁面中有任何回發時,如果隱藏字段值已更改,則將執行OnValueChangd事件。
所以你應該嘗試以下想法:
1)更新您的JavaScript for changevalue ,如下所示:
function changevalue(katoid)
{
document.getElementById('" + hidfldchosenkat.ClientID + "').value=katoid;
_doPostBack(); // this will force a PostBack which will trigger ServerSide OnValueChange
}
2)將HiddenField更改為TextBox但設置Style =“display:none;” 並設置AutoPostBack =“true” :
<asp:TextBox runat="server" ID="hidfldchosenkat"
Value="" Style="display:none;"
AutoPostBack="true" OnTextChanged="hidfldchosenkat_TextChanged">
</asp:TextBox>
這個例子對我很有用:
JavaScript的:
function changevalue()
{
$('#<%=hidfldchosenkat.ClientID%>').val("hi");
}
ASPX代碼:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="hidfldchosenkat" runat="server" AutoPostBack="true"
ontextchanged="hidfldchosenkat_TextChanged"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="Button" OnClientClick="changevalue()" />
</ContentTemplate>
</asp:UpdatePanel>
C#Code-Behind:
protected void hidfldchosenkat_TextChanged(object sender, EventArgs e)
{
string x = "hi"; // this fires when I put a debug-point here.
}
您的問題可能是:
document.getElementById('“+ hidfldchosenkat.ClientID +”')。value = katoid
你可能想嘗試:
$( '#<%= hidfldchosenkat.ClientID%>')VAL(katoid)。
你也應該在你的ASPX JavaScript標簽中輸入changevalue()而不是為每個LinkButton注冊它....所以請嘗試以下方法:
protected void lagerstyringgridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Assuming the LinkButtons are on the FIRST column:
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[0];
if (lb != null)
lb.Attributes.Add("onClick", "javascript:return changevalue(this);");
}
}
您可以通過使用JavaScript執行__doPostBack調用來激活任何事件。 以下是我如何使用它來允許我發送隱藏字段值服務器端的示例:
ASPX
<asp:HiddenField runat="server" ID="hfStartDate" ClientIDMode="Static" OnValueChanged="Date_ValueChanged" />
JavaScript的
$('#hfStartDate').val('send this');
__doPostBack('hfStartDate');
這將調用OnValueChanged事件並導致回發。 如果要進行部分回發,也可以將此設置為更新面板的觸發器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.