繁体   English   中英

HiddenField.ValueChanged从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()而不是为每个LinkBut​​ton注册它....所以请尝试以下方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM