开发者

asp.net binding problem

I have in my code behind the following property

public string Firstname {get;set;}

when I want to bind it to some textbox I do the following:

<asp:TextBox runat="server" ID="txtFirstname" Text='<%# Bind("Firstname") %>'/>

then I want value put in this textbox to be set in my Firstname property (because I want to process it e.g. save this value) in my presenter. Why it doesn't work? EDIT Here is the aspx

<formview runat="server" ID="myFormView">
                <p>Firstname <asp:TextBox ID="txtFirstName"  runat="server" Text='<%# Eval("Firstname") %>' /></p>
                <p>Lastname <asp:TextBox ID="txtL开发者_运维技巧astName" runat="server" /></p>
                <input type="button" title="send" runat="server" id="btnSend" />
            </formview>


It will bind in the page load but you have to tell it what to bind to in mark up or in code. You didn't say where or how you're storing your data and it sounds like you are trying to insert new data so...

Here is a tutorial on the sqldatasource. SQL Datasource Tutorial

Here is a turorial on the formview: Formview Tutorial

Here is a simple one I whipped up...(NOTE: I did not test the below code, so if I forgot somehting my apologies, but it should give you a good start).

 <asp:SqlDataSource ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="Connection string for your database here." 
    SelectCommand="SELECT FirstName, LastName FROM YourTable"
    >
</asp:SqlDataSource>

    <asp:FormView ID="frmYourForm" DefaultMode="Insert" runat="server" DataSourceID="SqlDataSource1">
        <EditItemTemplate>
            <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
            <br />
            <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
            <asp:LinkButton ID="LinkButton1" CommandName="Update" runat="server">Update</asp:LinkButton>
        </EditItemTemplate>
        <InsertItemTemplate>
            <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
            <br />
            <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
            <asp:LinkButton ID="LinkButton1" CommandName="Insert" runat="server">Insert</asp:LinkButton>
            </InsertItemTemplate>
    </asp:FormView>    

EDIT: Fixed the links to the tutorials...I didn't realize the orginal link didn't show info on the formview.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜