HiddenField.ValueChanged 从 Javascript 更改时不会触发事件

问题描述 投票:0回答:3

更新 我将 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
事件的情况下。)

不确定这是否是它的工作方式。

c# asp.net gridview hidden-field
3个回答
12
投票

隐藏字段不会产生回发(没有 AutoPostBack 属性),这意味着当隐藏字段的值发生更改时不会发生回发。但是,当页面有任何回发时,如果隐藏字段值已更改,则 OnValueChangd 事件将执行。

所以你应该尝试以下想法:

1) 更新您的 JavaScriptchangevalue,如下所示:

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# 代码隐藏:

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);

此外,您应该将 changevalue() 放入 ASPX JavaScript 标记内,而不是为每个 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);");
    }
}

5
投票

您可以通过使用 JavaScript 进行 __doPostBack 调用来触发任何事件。以下是我如何使用它来发送隐藏字段值服务器端的示例:

ASPX

<asp:HiddenField runat="server" ID="hfStartDate" ClientIDMode="Static" OnValueChanged="Date_ValueChanged" />

JavaScript

$('#hfStartDate').val('send this');
__doPostBack('hfStartDate');

这将调用 OnValueChanged 事件并导致回发。如果您想进行部分回发,您还可以将其设置为更新面板的触发器。


0
投票

另一个解决方案,纯 jQuery:

<asp:HiddenField runat="server" ID="hfStartDate" OnValueChanged="hfStartDate_ValueChanged" />

<script type="text/javascript">
    $('#<%=hfStartDate.ClientID%>').on('change', function () { __doPostBack(this.name) });
</script>  

然后使用/触发值的变化。

<script>    
    $('#<%=hfStartDate.ClientID%>').val("newvalue").trigger("change");
</script>
© www.soinside.com 2019 - 2024. All rights reserved.