Justin
Justin

Reputation: 885

Unable to DataBind asp:Button

I read online that if I want to make a data binding expression inline I have to call the databind method on the Page_Load function. However I am unable to access the button control in the code behind for some reason. I have access to all the other buttons on my form except the one I want. Here is some code:

 <asp:Button ID="CartButton" runat="server" Text="View Cart <%# Session["Counter"].ToString() %>" OnClick="List_Items"  />

and

 protected void Page_Load(object sender, EventArgs e)
    {
        CartButton.DataBind();
    }

This gives me an error that 'CartButton' does not exist in the current context. Running the page without the DataBind method call returns an error telling me that my

The server tag is not well formed.

Thanks for the help!

Upvotes: 0

Views: 1406

Answers (2)

DraggonZ
DraggonZ

Reputation: 1087

I think in this situation you don't need the databinging. Try to do something like that instead:

protected void Page_Load(object sender, EventArgs e)
{
    CartButton.Text = String.Format("View Cart {0}", Session["Counter"].ToString());
}

<asp:Button ID="CartButton" runat="server" OnClick="List_Items"  />

Upvotes: 1

AB Vyas
AB Vyas

Reputation: 2389

Try This

<asp:Button ID="Button1" runat="server" Text='<%# Session["Counter"].ToString() %>'/>

May this will help you.

Regards

AB Vyas

Upvotes: 1

Related Questions