views:

1373

answers:

3

hi guys,

  I have following in html

<div id="dvAddToGrid" runat="server">
                                    <table style="margin-left:80%">
                                        <tr>
                                            <td>
                                                <asp:LinkButton ID="lnkAddToGrid" runat="server" Text="Add New" 
                                                    onclick="lnkAddToGrid_Click" OnClientClick="GetValues()" Font-Underline="True"></asp:LinkButton>
                                            </td>
                                        </tr>
                                    </table>
                                    </div>

I have following in javascript

function GetValues() {

//    for (i = 1; i <= 5; i++) 
//    {
//        $("#hdnTableValues")[0].value += document.getElementById("txtSerialNo_1").value+ ",";
//        $("#hdnTableValues")[0].value += document.getElementById("txtBookName_1").value + ",";
//        $("#hdnTableValues")[0].value += document.getElementById("txtAuthor_1").value + ",";
//        $("#hdnTableValues")[0].value += document.getElementById("txtPublisher_1").value + ",";
//        $("#hdnTableValues")[0].value += document.getElementById("txtNoOfBooks_1").value + ",";
//        $("#hdnTableValues")[0].value += document.getElementById("txtRemarks_1").value + "|";
//           //    }
    document.getElementById("lblTableValues").innerHTML = $("#hdnTableValues")[0].value ;

}

In my code behind i have

 protected void lnkAddToGrid_Click(object sender, EventArgs e)
        {
            DataTable dtBookList = new DataTable();
            dtBookList.Columns.Add("SerialNo");
            dtBookList.Columns.Add("BookName");
            dtBookList.Columns.Add("Author");
            dtBookList.Columns.Add("Publisher");
            dtBookList.Columns.Add("NoOfBooks");
            dtBookList.Columns.Add("Remarks");
            string str = lblTableValues.Text ;
            for(int i=1;i<5;i++)
            {
                DataRow dtRow = dtBookList.NewRow();
                //hdnTableValues.Value 
            }
                       dvBookList.Visible = false;
            dvAddToGrid.Visible = false;

        }

Problem is i am getting values in lblTableValues in js.But in code behid it does not contain any values its value is "".Can anybody help to get the value contained in hdnTableValues in click event in code behind.

+2  A: 

You can use a hidden input with runat="server" to handle this. Store the value to the hidden field in JavaScript. And you can access the field value in C# code behind.

HTML

<input type="hidden" id="txtHidData" runat="server" />

JavaScript

document.getElementById ( "txtHidData" ).value = "your value";

C#

string valueInCodeBehind = txtHidData.Value;
rahul
still not getting in code behind
A: 

interesting...cool

Abu Hamzah
+1  A: 

I tried it but does not work. I have a hidden field which is set by javasript just as you have shown, but when i use hidden field in c# code to pass value to storedprocedure it does not pass it. I look at the page source and it does not have the value for the hiddenfield

amar