更新 我将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
事件也会发生同样的事情。)
不确定这是否意味着它的工作方式。
答案 0 :(得分:9)
隐藏字段不会产生回发(没有AutoPostBack属性),这意味着当隐藏字段的值发生更改时不会发生回发。但是,当页面中有任何回发时,如果隐藏字段值已更改,则将执行OnValueChangd事件。
所以你应该尝试以下想法:
1)更新更改值的 JavaScript ,如下所示:
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
您可能想尝试:
$( '#&LT;%= hidfldchosenkat.ClientID%GT;')。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);");
}
}
答案 1 :(得分:3)
您可以通过使用JavaScript执行 __ doPostBack 调用来解决任何事件。以下是我使用它来允许我发送隐藏字段值服务器端的示例:
<强> ASPX 强>
<asp:HiddenField runat="server" ID="hfStartDate" ClientIDMode="Static" OnValueChanged="Date_ValueChanged" />
<强>的JavaScript 强>
$('#hfStartDate').val('send this');
__doPostBack('hfStartDate');
这将调用OnValueChanged事件并导致回发。如果要进行部分回发,也可以将此设置为更新面板的触发器。