开发者

automatically load textbox into asp.net detailsview

Hi I'm trying to automatically pass a value from a textbox into开发者_运维知识库 a details view query but nothing is showing up. Heres what I've got:

ASP CODE:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

 </asp:Content>
 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
     <asp:TextBox ID="test" runat="server"></asp:TextBox>

&nbsp;
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
    DataSourceID="SqlDataSource1" Height="50px" Width="125px">
    <Fields>
        <asp:BoundField DataField="blogid" HeaderText="blogid" 
            SortExpression="blogid" />
        <asp:BoundField DataField="myfriendid" HeaderText="myfriendid" 
            SortExpression="myfriendid" />
        <asp:BoundField DataField="inputdate" HeaderText="inputdate" 
            SortExpression="inputdate" />
        <asp:BoundField DataField="content" HeaderText="content" 
            SortExpression="content" />
    </Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:test2ConnectionString %>" 
    SelectCommand="SELECT * FROM [BLOG] WHERE ([blogid] = @blogid)">
    <SelectParameters>
        <asp:FormParameter FormField="test" Name="blogid" Type="Double" />
    </SelectParameters>
</asp:SqlDataSource>
</asp:Content>

c# code:

namespace Log_In.Account
{
     public partial class Page2_1 : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)

        {

        string EmpId = Request.QueryString["blog_ID"];
        test.Text = blog_ID;
        DetailsView1.DataBind();
    }
}

}

Any ideas on how i can fix this?


Actually there are a few errors in your code.

Arguably the main error is that you are use FormParameter for referencing test textbox value. Such technique works well only if that textbox or another referenced server control placed in form tag directly without no parent server controls those implement INamingContainer interface like asp:ContentPlaceholder control. In other words you can't use FormParameter if you use a master pages.

That's because each parent control that implements INamingContainer participates in building children control's UniqueIDs. Those ids used for retrieving posted values from the Request.Form dictionary. So as far as the test textbox placed in a Content control it's UiqueID looks like ctl00$MainContent$test . Of course you set this id on FormParameter's FormField property value but this looks very ugly.

To resolve this issue you may use a ControlParameter instead of FormParameter. like this:

<asp:ControlParameter ControlID="test" Name="blogid" 
    PropertyName="Text" Type="Double" />

The second error is maybe the most common error in data binding : you are not wrap data binding code by !IsPostback condition check.

But even you will add check mentioned above your code still will not work well with a FormParameter as the Request.Form dictionary already formed from values of form elements posted to the HTTP request body, with a form using the POST method.

To resolve this error you can rewrite Page_Load body as below with conduction of using ControlParameter:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        test.Text = Request.QueryString["blog_ID"];
    }
}

Draw attention that you don't need to call DetailsView1.DataBind method explicitly as all page's controls databindings processed after Page_Load.

If it's was too hard to understand my English :) please follow this link to realize difference between FormParameter and ControlParameter: SqlDataSource control and Master Page problem

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜