C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...







DropDownList controls in a Gridview


Posted Date: 16 Jul 2008    Resource Type: Code Snippets    Category: ASP.NET GridView

Posted By: Jessie       Member Level: Gold
Rating:     Points: 8



The sample code load values in dropdown2(dd2) based on the selected value in dropdown1(dd1)

The code below is that of an .aspx page that contains the DropDownList controls in a GridView control


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=false OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
DropDown 1
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="dd1" runat=server DataSource='<%#Load_DropDown()>' DataTextField="brand_name" DataValueField="brand_id"
OnSelectedIndexChanged="Populate_Grid2" AutoPostBack=true> </asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<HeaderTemplate>
DropDown 2
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="dd2" runat=server DataTextField="brand_name" DataValueField="brand_id"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField> </Columns>
</asp:GridView>


The code behind page handles the event that populates data in GridView





//function to determine which one is selected in dd1
protected void Populate_Grid2(object sender, EventArgs e)
{

//locate the row in which the dropdown value has been changed
GridViewRow gr=(GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent;
//find the control in that
DropDownList d1=(DropDownList) gr.FindControl("dd1");
string selectedvalue=d1.selectedvalue;

//using selectedvalue execute a query like
//select * from product where brand_id=selectevalue
//get the result in datatable dt

//located the second dropdown(dd2)
DropDownList d2 = (DropDownList)gr.FindControl("dd2");
d2.DataSource = dt;
d2.DataBind();

}






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
SelectedIndexChanged Event of a DropDownList control  .  Populating DropDownList controls  .  Dropdownlist selectedIndexChanged inside gridview  .  DropDownList controls in a GridView control  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Repeater with CheckBox nested inside a GridView
Previous Resource: Select, Delete, Cancel GridView rows in ASP.NET
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use