开发者

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

开发者 https://www.devze.com 2023-04-06 21:29 出处:网络
This is my aspx code <asp:TemplateField HeaderText=\"Column Name\"> <ItemTemplate> <asp:TextBox ID=\"TextBox1\" runat=\"server\" AutoPostBack=\"false\" ></asp:TextBox>

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 te开发者_如何转开发xtbox 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..


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消