Mahendra
Mahendra

Reputation: 11

How should I give text to textbox which will displayed in textbox at runtime

This is my aspx code

<asp:TemplateField HeaderText="Column Name">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="false" ></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

this is my cs code

 int rowIndex =0;
        TextBox box1=new TextBox();
        box1.Text = ((TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox1")).Text;

Normally if we want to give value to textbox we give lkie this

 <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="false" Text="SomeText"></asp:TextBox>

but now I have textbox in gridview so I am accessing that as written above cs code. I want to give text to textbox from cs code. then How should I give text to textbox which will displayed in textbox at runtime..

Upvotes: 0

Views: 420

Answers (3)

Sagar Kadam
Sagar Kadam

Reputation: 485

TextBox1.text = "Some Text" Since U have Already Give Some ID to the TextBox Assumption Second U have Stated it will runat Server so it is Accessible at CS File.

Upvotes: 0

VinayC
VinayC

Reputation: 49195

It will be the same way you have got the text:

((TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox1")).Text = "SomeText";

But typically, the text for each row will change and folks use data-binding declarative syntax to assign text - for example

<asp:TemplateField HeaderText="Column Name">
  <ItemTemplate>
     <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("PropertyOrColumnName") %>'></asp:TextBox>
  </ItemTemplate>
</asp:TemplateField>

See data binding overview for quick start.

Upvotes: 0

Serhat Ozgel
Serhat Ozgel

Reputation: 23766

Instead of

int rowIndex =0;
TextBox box1=new TextBox();
box1.Text = ((TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox1")).Text;

I think you should do this:

int rowIndex =0;
TextBox box1 = ((TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox1"));
box1.Text = "Whatever you want to display";

You may do this only after you bind the data to your grid.

Upvotes: 1

Related Questions