Borut Flis
Borut Flis

Reputation: 16415

Set selectparameter from textbox in ASP.NET

<SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="model"  Type="String" />
            </SelectParameters>

My program actually works, I am just curious to find out why. I have a textbox with ID TextBox1 and a SqlDataSource that requires its value as a parameter in a query. So I set the source of the parameter as displayed above. I also have a button near the textbox, a click on which sets the value of the parameter. Why is that? The button has no onclick event.

Upvotes: 0

Views: 2161

Answers (1)

Denys Wessels
Denys Wessels

Reputation: 17049

Because clicking the button causes a postback to occur and your SelectCommand in the SqlDataSource gets re-constructed and if you've entered anything in the textbox it appends this value to the query.

After this happens your databound control (grid view,details view) refreshes displaying new values.

It's not magic, you just need to understand that ASP.NET is stateless and every time a postback occurs the page goes through the ASP.NET Page life cycle and your controls are re-created

Upvotes: 2

Related Questions