开发者

Bind text box to value

How to bind to textbox and retrieve modified value on button click.

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Sku objSku = new Sku();
            objSku.Name = "Test";
            Sku = objSku;
            frm.DataSource = Sku;
            frm.DataBind();
        }
        if (IsPostBack)
        {

            Sku = ViewState["Sku"] as Sku;
        }

    }

    public Sku Sku { get; set; }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Hers sku.Name should be equal to the value entered by the user . so that I can save the object.

    }

}
[Serializable]
public class Sku 
{
    public string Name { get; set; }
}

Html code

<form id="form1" runat="server">
    <div>
    <asp:FormView runat="server" ID="frm" >
  开发者_运维知识库  <ItemTemplate>


        </ItemTemplate>
    </asp:FormView>
    <asp:TextBox ID="TextBox1" Text='<%# Bind("Name") %>' runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    </form>


You have direct access to your textbox as it's not in the FormView so:

String val= Textbox1.Text;
Textbox1.Text="what ever you are binding?"

I think you may need to be a little clear in your question as I don't understand what you are trying to do?


It looks like you're already binding to the field for displaying data. Is this not working? Within your button click event handler, you can retrieve the current value from TextBox1.Text.


If you want to update your view on postback you need to update the bindings.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Sku objSku = new Sku();
        objSku.Name = "Test";
        Sku = objSku;
    }
    if (IsPostBack)
    {

        Sku = ViewState["Sku"] as Sku;
    }
    frm.DataSource = Sku;
    frm.DataBind();

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜