tags:

views:

477

answers:

4

I am looking into using only a ddl to run my query, not a ddl and a Button_Click function. I am yet to find what to do. How do I do this?

+2  A: 

Set the AutoPostBack property to true, then hook into the OnSelectedIndexChanged event

<asp:DropDownList 
         id="dropDownList1" 
         runat="server" 
         AutoPostBack="true" 
         OnSelectedIndexChanged="dropDownList1_SelectedIndexChanged" />

Server Side

void dropDownList1_SelectedIndexChanged
                   (Object sender, EventArgs e) {

   //run your query

}
Bob
A: 

Ensure your Drop down list has it's "AutoPostback" property set to true. This will cause the page to post back when the user selects an item from within the drop-down list. You can respond to this in your code-behind in whichever event you desire, Page_Load, or the DDL's own OnSelectedIndexChanged

CraigTP
+5  A: 

In your as(p/c)x:

<asp:DropDownList runat="server" 
                  id="ddl"
                  OnSelectedIndexChanged="SelectionChanged"
                  AutoPostBack="true">
    <asp:ListItem Text="Page 1" Value="/page1.aspx" />
    <asp:ListItem Text="Page 2" Value="/page2.aspx" />
</asp:DropDownList>

The "AutoPostBack" property tells ASP.NET to wire up a client-side (javascript) command that submits the form as soon as the drop down list changes, instead of waiting for a button click.

And in your codebehind, the event handler we referenced in the "OnSelectedIndexChanged" property will get fired:

protected void SelectionChanged(object sender, EventArgs e)
{
    Response.Redirect(((DropDownList)sender).SelectedValue);
}
Rex M
A: 

Thank you very much! for all of you. The one who posted the question and all of you guys who devoted your knowledge to this forum.

Thank you again!

tuulet