Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ASP.NET
<asp:GridView ID="GridView1" runat="server" CellPadding="1" CellSpacing="1"  DataKeyNames="PostID"
                       AutoGenerateColumns="False" Width="98%" OnRowCommand="GridView1_RowCommand"
                       AllowPaging="True" ShowHeader="false" onrowdatabound="GridView1_RowDataBound" BackColor="White" RepeatLayout="Table" BorderColor="#B0E3FF" BorderStyle="None" BorderWidth="1px" style="margin-left: 9px; height:auto">
 
 <asp:Button ID="btnComnt" runat="server" OnClick="btnComnt_Click" class="btnComment" Text="Comment" />
                 <asp:HiddenField ID="HiddenField1" Value='<%#Eval("PostID") %>' runat="server" />   
 
 </asp:GridView>

I do get the data into the HiddenField .

C#
protected void btnComnt_Click(object sender, EventArgs e)
        {
            int userid = int.Parse(Session["UnAuthentication"].ToString());
            string date = DateTime.Now.ToString();
            int privates = 0;

            TextBox txtNewComent = FindControl("txtComent") as TextBox;
            HiddenField postNo = FindControl("HiddenField1") as HiddenField;

                Post post = new Post();
                post.Comment = new Comments(int.Parse(postNo.Value), txtNewComent.Text, userid);
                dll.PostReply(post);
              
            
        }


I want to add the values from TextBox and HiddenField when i am adding the Post.

Please help me with the code to take the values from the HiddenField on GridView to postNo.
I can populate value to HiddenField but i cannot read it back from code behind
Posted
Updated 20-Sep-14 20:36pm
v2
Comments
Kornfeld Eliyahu Peter 21-Sep-14 4:55am    
The mark-up you posted here not even valid!!!
[no name] 21-Sep-14 4:58am    
i do have a full make up, but what i want you to focus on the most is the code behind cos that's what you gonna need to solve this problem.
Kornfeld Eliyahu Peter 21-Sep-14 5:00am    
Remember that all I have is what you posted here - so when you simplify your code (and that is a good idea to do) you must take care that it still will be valid...
According to your comment I assume that your button and hidden field are in a row template? Am I right?
[no name] 21-Sep-14 5:06am    
Thank for reading my question. I got what i was looking for but the only prob now is that is looping several time and everytime it loop, it add a multiple records...

1 solution

Thanx to you who tryed to solve or have time to read my question. Here is the solution i was looking for

C#
foreach (GridViewRow row in GridView1.Rows)
          {

              string textBoxText = ((TextBox)row.FindControl("txtComent")).Text;

              string userId = ((HiddenField)row.FindControl("HiddenField1")).Value;




                      Post post = new Post();
                      post.Comment = new Comments(int.Parse(userId), textBoxText.ToString(), userid);
                      dll.PostReply(post);
                      this.PopulateData();


          }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900