9

UPDATE I moved the Javascript to the ASPX site instead, and added a postback function for it. Now it works. Thanks to @orgtigger and especially @lucidgold for spending their time helping me!

Here is the update code that works!

<script type="text/javascript">
    function changevalue(katoid) {
        $('#<%=txtboxchosenkat.ClientID%>').val(katoid);
        __doPostBack('<%= updpnlgridview.ClientID %>', '');
    }
</script>

Code:

<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

Code-behind:

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

The links (only part of code that makes links):

  string line = String.Format("<li><a href='#' onclick='changevalue(" + pid + ");'>{0}", 
  menuText + "</a>");

Old Post I need to update a GridView based on the value of a HiddenField. I am currently using a button to populate the GridView, but would like to do it automaticaly as soon as the value in the HiddenField changes.

But when I change the value with a javascript, then event doesn't fire.

(Same thing also happens in case of a TextBox and its OnTextChanged event.)

Not sure if this is way it's meant to work.

Joe
  • 93
  • 1
  • 1
  • 6
  • Is there some reason why you have to change the value instead of just calling the event in the code? instead of TextBox1.Text = "1"; just using hidfldchosenkat_ValueChanged(sender,e); – orgtigger Jun 16 '14 at 17:44
  • @orgtigger Yes, because the code Selects * from the tblProducts where the ID matches the value from the hiddenfield. And I have some links that changes the value of the hidden field. See the new update. – Joe Jun 16 '14 at 17:48
  • The other suggestion I will make then is to call the event AFTER you change the hidden value in the code. (since nothing in the event arguments requires passing values to it) – orgtigger Jun 16 '14 at 18:18
  • @orgtigger How will I go about that? I will have to call the event along with the javascript when I click on the "A href onclick=''" link. But i have tried that by running another javascript that does that. so the A href onclick actually has to run two javascripts ;) – Joe Jun 16 '14 at 18:23
  • us/library/system.web.ui.webcontrols.hiddenfield.valuechanged(v=vs.110).aspx and everything looks like it should work (YOu did place your runat earler in the hiddenfield but that should not make a difference). THe only other question I would ask is if the update only fails to happen the first time, or if you try to update again the process works (Related to not declaring the value). Good luck sir. – orgtigger Jun 16 '14 at 19:00
  • Is pid a column of lagerstyringgridview? If so, what is its index? – lucidgold Jun 16 '14 at 19:31

2 Answers2

12

A Hidden field will not produce a postback (there is no AutoPostBack property), which means no postback will happen when the value of the hidden field has changed. However, when there is ANY postback from the page then OnValueChangd event will execute if a hidden field value has changed.

So you should try the following ideas:

1) Update your JavaScript for changevalue as follows:

function changevalue(katoid)
{
    document.getElementById('" + hidfldchosenkat.ClientID + "').value=katoid;
    _doPostBack();  // this will force a PostBack which will trigger ServerSide OnValueChange
}

2) Change your HiddenField to a TextBox but set the Style="display:none;" and set AutoPostBack="true":

<asp:TextBox runat="server" ID="hidfldchosenkat" 
             Value="" Style="display:none;" 
             AutoPostBack="true" OnTextChanged="hidfldchosenkat_TextChanged">
</asp:TextBox>

This example works great for me:

JavaScript:

function changevalue()
{
    $('#<%=hidfldchosenkat.ClientID%>').val("hi"); 
}

ASPX Code:

<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.
}

Your issue could be with:

document.getElementById('" + hidfldchosenkat.ClientID + "').value=katoid

You may want to try:

$('#<%=hidfldchosenkat.ClientID%>').val(katoid);

Also you should PUT changevalue() inside your ASPX JavaScript tags and not register it for every LinkButton.... so instead try the following:

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);");
    }
}
lucidgold
  • 4,432
  • 5
  • 31
  • 51
  • Im sorry, I just changed the headline. What im doing is actually changing the value of the hidden field with Javascript. – Joe Jun 16 '14 at 18:10
  • I have tried doing this with a textbox, but the event "textchanged" doesn't trigger either, even though I set the AutoPostBack to true. it's like it doesn't recognize that it's being changed. Also, the _DoPostback(); didn't work. I put it together with my other javascript like this: `code` ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "", false); – Joe Jun 16 '14 at 18:39
  • Are you sure when you change to a Textbox it didnt work? Did you declare hidfldchosenkat_TextChanged?? – lucidgold Jun 16 '14 at 18:43
  • Yes, it does work if i just write the text in the textbox and then click outside afterwards to remove focus. then it makes the postback. but when it gets changed from the javascript, it doesn't trigger the autopostback. – Joe Jun 16 '14 at 18:46
  • I tried this one more time, I can see that the text in the textbox is changing when I click the links. But the postback never happens. Then if I change the text in the textbox manually and remove focus by clicking somewhere else on the site, the postback happens. Just like before. The textchanged event doesn't trigger when it's being changed from javascript. Could it have something to do with that it's actually the value im changing.. txtboxchosenkat.ClientID + "').value=katoid – Joe Jun 16 '14 at 19:01
  • Look at the example I have prvided because its working for me. Try to use $('#<%=hidfldchosenkat.ClientID%>').val(katoid); instead of document.getElementById('" + hidfldchosenkat.ClientID + "').value=katoid.... Please post your updated code. – lucidgold Jun 16 '14 at 19:07
  • Or that its in an updatepanel, I have to try and fiddle with the trigger to see if that makes any difference ;) – Joe Jun 16 '14 at 19:07
  • I can't get this to work: "" Drives me crazy.. – Joe Jun 16 '14 at 19:15
  • You are missing { and } for the function – lucidgold Jun 16 '14 at 19:16
  • ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp", "", false); – Joe Jun 16 '14 at 19:19
  • You really should have the fuction inside the ASPX page rather than regestering it with ScriptManager. – lucidgold Jun 16 '14 at 19:20
  • I have moved the script to the ASPX page, and it's both updating the textbox and making a postback ;) but.. it's making a postback on the whole page, not just the updatepanel. I'm working on that. – Joe Jun 16 '14 at 19:37
  • Ohh okay, you may want to remove _doPostBack(); to fix that if you still have it in your code – lucidgold Jun 16 '14 at 19:38
  • You need to show your updated code so I can help you at this point, not sure what you have tried. Edit your Question and update your code – lucidgold Jun 16 '14 at 19:46
  • 1
    Holy s**t it works. I changed the Javascript to this: – Joe Jun 16 '14 at 19:48
  • Nice job! If my answer has helped you in any way, feel free to accept it or upvote it! – lucidgold Jun 16 '14 at 19:50
  • You have really helped me alot, been trying to do this for many many hours :O so thank you very much for using your time to help me! – Joe Jun 16 '14 at 19:53
5

You can have any event fire by doing a __doPostBack call with JavaScript. Here is an example of how I used this to allow me to send a hiddenfield value server side:

ASPX

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

JavaScript

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

This will call the OnValueChanged event and cause a postback. You can also set this is a trigger for an update panel if you would like to do a partial postback.