Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a textbox inside a gridview. I will set the text of the textbox in runtime.

Now I want to access the text of the textbox on click event of a button outside gridview.

HTML:

 <asp:GridView runat="server" Id="grdRuleFieldMapping" AutoGenerateColumns="false">
<columns>
<asp:BoundField HeaderText="Field Mapping" DataField="FiledMapping">
<asp:TemplateField HeaderText="Mapping Text">                     <itemtemplate><asp:TextBox runat="server" ID="txtMappingText"></itemtemplate><asp:TemplateField><columns>
        
        <asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click"></columns></columns>


Code-Behind:

protected void btnSubmit_Click(object sender, EventArgs e)
       {
           foreach (GridViewRow grdRow in grdRuleFieldMapping.Rows)
           {
               TextBox objTxt1 = (TextBox)grdRow.Cells[1].FindControl("txtMappingText");
           }
       }
Posted
Updated 6-Jan-11 20:16pm
v2
Comments
Sandeep Mewara 7-Jan-11 2:25am    
So, whats the problem?

That's right way as you're finding textbox.

I am not finding any Error of something wrong in it. If you're finding any then you can state it here.

But Let me tell you about some expansion to your code for getting textbox value

C#
protected void btnSubmit_Click(object sender, EventArgs e)
       {
           foreach (GridViewRow grdRow in grdRuleFieldMapping.Rows)
           {
               TextBox objTxt1 = (TextBox)grdRow.Cells[1].FindControl("txtMappingText");
//Check if TextBox is not null
if(objTxt1 != null)
{
string reqText = objTet1.Text;
}
           }
       }


Hope you were searching for the same thing.
 
Share this answer
 
Comments
Sandeep Mewara 7-Jan-11 2:29am    
Good answer! 5!
Dalek Dave 7-Jan-11 3:31am    
Good Answer.
karthikkvishwanath 7-Jan-11 4:56am    
I am unable to get the Text. It is "". I mean the textbox is not null. But the Text property is empty.

i.e., objTxt1.Text = "";
thatraja 7-Jan-11 13:49pm    
Good answer
Dear Friend ,

The text box should inside the
<br />
<asp:TemplateField HeaderText="Mapping Text"><br />
<Itemtemplate><br />
<asp:textbox runat="server" id="txtMappingText" > <br />
<br />
<br />
</Itemtemplate><br />

but sorry are you missing binding data to the Textbox , aren't you ?
any way you should bind text data to access them in the Text attribute
<br />
<asp:textbox runat="server" id="txtMappingText" text="<%# Eval("Feild Name")%>" ><br />


and if you want to set data you just set it when you get the objTxt1 by

<br />
objTxt1.Text="Your Value";<br />


regards
Ab Abulubbad
 
Share this answer
 
v4
Comments
karthikkvishwanath 7-Jan-11 13:22pm    
The textbox is actually inside templatefield only.
Dymanically the value is set. I mean the user accessing the page will enter the text inside the textbox, which must be available in the code-behind
aha now i got it , look you should put your databiding function if its inside the Page_Load()
inside
<br />
if(!IsPostBack)<br />
{<br />
//BidingFunction<br />
}<br />


your not getting the user valuse because you binding data again when the page is posted back .

AB Abulubbad
 
Share this answer
 
Comments
karthikkvishwanath 9-Jan-11 11:49am    
Hey, you are absolutely right. I am being a fool till this time... Ooops had gone blind with the basics.

Thanks a ton :)
imaa2amha 9-Jan-11 16:01pm    
your most welcome

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