Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I change my asp:GridView contents when I change my selected item in an asp:DropDownList?

Posted on 2014-01-17
3
Medium Priority
?
114 Views
Last Modified: 2015-02-02
When I select a new item in my asp:DropDownList, I want the data in my asp:GridView to reflect info based on the newly selected item in my asp:DropDownList.

I'm using VS2012, .NET Framework 4.5 and C# (HTML 5.0 and CSS 3.0)

Seems like it should be simple ;-) ... please help me understand what I am missing!!

Thanks in advance:

Here's my C# codebehind, followed by my aspx with an Ajax Update Panel, dropdown, and gridview...

my aspx.cs:

protected void ddlTaskList_SelectedIndexChanged(object sender, EventArgs e)
{
    //dsMail.UpdateParameters
    //lblCreateDistList.Visible = true;
    dsMail.UpdateParameters.Add("@TaskID", DbType.Int32, ddlTaskList.SelectedValue);
    grdEmails.DataSource= dsMail;
    grdEmails.DataBind();


}

Open in new window

my aspx:

   
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>
            <asp:asyncpostbacktrigger controlid="ddlTaskList" eventname="SelectedIndexChanged" />
        </Triggers>
    <ContentTemplate>
        <asp:DropDownList ID="ddlTaskList" AutoPostBack="true" OnSelectedIndexChanged="ddlTaskList_SelectedIndexChanged" runat="server"></asp:DropDownList>

        <asp:SqlDataSource ID="dsMail" runat="server" SelectCommandType="StoredProcedure" ConnectionString="<%$ ConnectionStrings:EagleEyeConnectionString %>" SelectCommand="stpGetEmailDataByTaskID">
            <SelectParameters>
                <asp:FormParameter DefaultValue="1" FormField="ddlTaskList.DataValueField" Name="TaskID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="grdEmails" Runat="server" 
            AutoGenerateColumns="False" 
            SkinID="Professional">
            <Columns>
                <asp:HyperLinkField HeaderText="NameLnk" DataTextField="Name" DataNavigateUrlFields="Name" 
                    DataNavigateUrlFormatString="#?Name={0} " />
                <asp:BoundField DataField="TaskDesc" HeaderText="TaskDesc" ReadOnly="True" SortExpression="TaskDesc" />
            </Columns>
        </asp:GridView>

      </ContentTemplate>
  </asp:UpdatePanel>

Open in new window

0
Comment
Question by:aflcio-hit
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 39790645
Since you already have dropdownlist bound as a selectparameter for the grid, you should only need to call DataBind() on the grid. There should be no need to alter the datasource at all.
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 39790669
I would just add AppendDataBoundItems="True" to the dropdownlist.

        <asp:DropDownList ID="ddlTaskList" AutoPostBack="true" OnSelectedIndexChanged="ddlTaskList_SelectedIndexChanged" runat="server" AppendDataBoundItems="True"></asp:DropDownList>

Open in new window

0
 

Author Closing Comment

by:aflcio-hit
ID: 40584561
Thanks. Sorry for the slow response.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question