Как запустить dropdownlist selectedIndexChanged на EditItemTemplate в GridView.... pls помогите мне решить эту проблему...я новичок в asp.net...thnx заранее
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> </asp:Content> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <title>Register:</title> <asp:Label ID="lblFb" runat="server" /> <center> <table cellpadding="10px"> <tr> <td> Name: </td> <td> <asp:TextBox ID="txtName" runat="server" /> </td> </tr> <tr> <td> Mobile: </td> <td> <asp:TextBox ID="txtMobile" runat="server" /> </td> </tr> <tr> <td> Age: </td> <td> <asp:TextBox ID="txtAge" runat="server" /> </td> </tr> <tr> <td> DoB: </td> <td> <asp:TextBox ID="txtDoB" runat="server" /> </td> </tr> <tr> <td>Country</td> <td><asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" onselectedindexchanged="ddlCountry_SelectedIndexChanged"> </asp:DropDownList></td> </tr> <tr> <td>State</td> <td><asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" onselectedindexchanged="ddlState_SelectedIndexChanged"> <asp:ListItem Text="--select state--"></asp:ListItem> </asp:DropDownList></td> </tr> <tr> <td>City</td> <td><asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true"> <asp:ListItem Text="--select city--"></asp:ListItem> </asp:DropDownList></td> </tr> <tr> <td colspan="2"> <asp:Button ID="btnSubmit" Text="Submit" runat="server" Font-Bold="true" OnClick="btnSubmit_Click" /> </td> </tr> </table> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="100%" DataKeyNames="ID" BackColor="#DEBA84" AutoGenerateColumns="false" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="5" CellSpacing="2" onrowediting="GridView1_RowEditing" onrowcancelingedit="GridView1_RowCancelingEdit" onrowupdating="GridView1_RowUpdating" onrowdatabound="GridView1_RowDataBound"> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <Columns> <asp:TemplateField HeaderText="Sr. No"> <ItemTemplate> <%#Container.DisplayIndex+1 %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <EditItemTemplate> <asp:TextBox ID="txtEditName" runat="server" Text='<%#Eval("Name")%>' /> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblItemName" runat="server" Text='<%#Eval("Name") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Mobile"> <EditItemTemplate> <asp:TextBox ID="txtEditMobile" runat="server" Text='<%#Eval("Mobile") %>' /> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblItemMobile" runat="server" Text='<%#Eval("Mobile")%>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Age"> <EditItemTemplate> <asp:TextBox ID="txtEditAge" runat="server" Text='<%#Eval("Age") %>' /> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblItemAge" runat="server" Text='<%#Eval("Age") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Date of Birth"> <EditItemTemplate> <asp:TextBox ID="txtEditDoB" runat="server" Text='<%#Eval("DoB") %>' /> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblItemDoB" runat="server" Text='<%#Eval("DoB") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Country"> <EditItemTemplate> <asp:DropDownList ID="ddleditCountry" runat="server" OnSelectedIndexChanged="ddlEditCountry_selectedIndexChange" AutoPostBack="true" /> <asp:HiddenField ID="hdnCountry" runat="server" Value='<%#Eval("Country") %>' /> </EditItemTemplate> <ItemTemplate> <center> <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>' /> </center> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="State"> <EditItemTemplate> <asp:DropDownList ID="ddleditState" runat="server" /> </EditItemTemplate> <ItemTemplate> <center> <asp:Label ID="lblState" runat="server" Text='<%#Eval("State") %>' /> </center> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <EditItemTemplate> <asp:DropDownList ID="ddleditCity" runat="server" /> </EditItemTemplate> <ItemTemplate> <center> <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>' /> </center> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Edit"> <EditItemTemplate> <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" /> <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" /> </EditItemTemplate> <ItemTemplate> <asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" CommandName="Edit" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Delete"> <ItemTemplate> <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" OnClick="GridView1_RowDeleting" /> </ItemTemplate> </asp:TemplateField> </Columns> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BorderColor="#FF99FF" /> </asp:GridView> </center> </asp:Content>
Код CS:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection(@"server=DELL\SQLEXPRESS; Database=Bussiness; user id=sa; Password=vaishnavisingh;"); SqlParameter[] prm; SqlDataAdapter da = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand(); DataSet ds = new DataSet(); string str = ""; static int id = 0; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { bindGrid(); bindcountry(); } } protected void bindGrid() { con.Open(); cmd = new SqlCommand("Select * From Grid", con); da = new SqlDataAdapter(cmd); ds = new DataSet(); da.Fill(ds); con.Close(); if (ds.Tables[0].Rows.Count > 0) { GridView1.DataSource = ds; GridView1.DataBind(); } } protected void btnSubmit_Click(object sender, EventArgs e) { prm = new SqlParameter[7]; prm[0] = new SqlParameter("@Name", txtName.Text.Trim()); prm[1] = new SqlParameter("@Mobile", txtMobile.Text.Trim()); prm[2] = new SqlParameter("@Age", txtAge.Text.Trim()); prm[3] = new SqlParameter("@DoB", txtDoB.Text.Trim()); prm[4] = new SqlParameter("@Country", ddlCountry.SelectedItem.Text); prm[5] = new SqlParameter("@State", ddlState.SelectedItem.Text); prm[6] = new SqlParameter("@City", ddlCity.SelectedItem.Text); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "insertData"; cmd.Connection = con; cmd.Parameters.AddRange(prm); con.Open(); int result = cmd.ExecuteNonQuery(); con.Close(); if (result > 0) { Response.Write("Registered successfully"); Response.Redirect("Default.aspx"); } } protected void GridView1_RowDeleting(object sender, EventArgs e) { ImageButton lnk = ((ImageButton)(GridView1.FindControl("imgbtnDelete"))); lnk = (ImageButton)sender; GridViewRow gvr = (GridViewRow)lnk.NamingContainer; String str = GridView1.DataKeys[gvr.RowIndex].Value.ToString(); int id = Convert.ToInt32(str); prm = new SqlParameter[1]; prm[0] = new SqlParameter("@id", id); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "deleteGrid"; cmd.Connection = con; con.Open(); cmd.Parameters.AddRange(prm); int result = cmd.ExecuteNonQuery(); if (result > 0) { Response.Redirect("Default.aspx"); } } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bindGrid(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bindGrid(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow gvr = GridView1.Rows[e.RowIndex]; String str = GridView1.DataKeys[gvr.RowIndex].Value.ToString(); int id = Convert.ToInt32(str); String editName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditName")).Text; String editMobile = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditMobile")).Text; String editAge = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditAge")).Text; prm = new SqlParameter[2]; prm[0] = new SqlParameter("@id", id); prm[1] = new SqlParameter("@RoomType", editName); Response.Redirect("addRoomType.aspx"); //Its Ryt Code// } protected void bindcountry() { ddlCountry.AppendDataBoundItems = true; ddlCountry.Items.Add(new ListItem("--Select Country--", "")); String strQry = "select Countryid, countryname from country"; cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = strQry; cmd.Connection = con; try { con.Open(); ddlCountry.DataSource = cmd.ExecuteReader(); ddlCountry.DataTextField = "CountryName"; ddlCountry.DataValueField = "CountryID"; ddlCountry.DataBind(); } catch (Exception ex) { throw ex; } finally { con.Close(); con.Dispose(); } } protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { ddlState.Items.Clear(); ddlState.Items.Add(new ListItem("--select state--", "")); ddlCity.Items.Clear(); ddlCity.Items.Add(new ListItem("--select city--", "")); ddlState.AppendDataBoundItems = true; String Qry = "Select StateID, StateName from State where CountryID=@CountryID"; cmd = new SqlCommand(); cmd.Parameters.AddWithValue("@CountryID", ddlCountry.SelectedItem.Value); cmd.CommandType = CommandType.Text; cmd.CommandText = Qry; cmd.Connection = con; try { con.Open(); ddlState.DataSource = cmd.ExecuteReader(); ddlState.DataTextField = "StateName"; ddlState.DataValueField = "StateID"; ddlState.DataBind(); if (ddlState.Items.Count > 1) { ddlState.Enabled = true; } else { ddlState.Enabled = false; ddlCity.Enabled = false; } } catch (Exception ex) { throw ex; } finally { con.Close(); con.Dispose(); } } protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) { ddlCity.Items.Clear(); ddlCity.Items.Add(new ListItem("--select city--", "")); ddlCity.AppendDataBoundItems = true; String Qry = "select Cityid, cityname from city where Stateid=@StateID"; cmd = new SqlCommand(); cmd.Parameters.AddWithValue("@StateID", ddlState.SelectedItem.Value); cmd.CommandType = CommandType.Text; cmd.CommandText = Qry; cmd.Connection = con; try { con.Open(); ddlCity.DataSource = cmd.ExecuteReader(); ddlCity.DataTextField = "CityName"; ddlCity.DataValueField = "CityID"; ddlCity.DataBind(); if (ddlCity.Items.Count > 1) { ddlCity.Enabled = true; } else { ddlCity.Enabled = false; } } catch (Exception ex) { throw ex; } finally { con.Close(); con.Dispose(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (GridView1.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddllist1 = ((DropDownList)e.Row.FindControl("ddleditCountry")); var hdnCountryName = ((HiddenField)e.Row.FindControl("hdnCountry")); ddllist1.AppendDataBoundItems = true; String strQry = "select Countryid, countryname from country"; cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = strQry; cmd.Connection = con; try { con.Open(); ddllist1.DataSource = cmd.ExecuteReader(); ddllist1.DataTextField = "CountryName"; ddllist1.DataValueField = "CountryID"; ddllist1.DataBind(); ddllist1.Items.Insert(0,new ListItem("--Select Country--", "0")); ddllist1.Items.FindByText(hdnCountryName.Value).Selected=true; } catch (Exception ex) { throw ex; } finally { con.Close(); con.Dispose(); } } } protected void ddlEditCountry_selectedIndexChange(object sender, EventArgs e) { var ddl1 = ((DropDownList)(GridView1.FindControl("ddleditCountry"))); ddl1 = (DropDownList)sender; GridViewRow gvr = (GridViewRow)ddl1.NamingContainer; ddlState.Items.Clear(); ddlState.Items.Add(new ListItem("--select state--", "")); ddlCity.Items.Clear(); ddlCity.Items.Add(new ListItem("--select city--", "")); ddlState.AppendDataBoundItems = true; String Qry = "Select StateID, StateName from State where CountryID=@CountryID"; cmd = new SqlCommand(); cmd.Parameters.AddWithValue("@CountryID", ddlCountry.SelectedItem.Value); cmd.CommandType = CommandType.Text; cmd.CommandText = Qry; cmd.Connection = con; try { con.Open(); ddlState.DataSource = cmd.ExecuteReader(); ddlState.DataTextField = "StateName"; ddlState.DataValueField = "StateID"; ddlState.DataBind(); if (ddlState.Items.Count > 1) { ddlState.Enabled = true; } else { ddlState.Enabled = false; ddlCity.Enabled = false; } } catch (Exception ex) { throw ex; } finally { con.Close(); con.Dispose(); } } }
Christian Graus
Если вы новичок в этом деле ASP.NET-тогда это слишком сложно для тебя. ASP.NET кроме того, он далеко не так хорош, как MVC, так что просто переходите на MVC, очевидно, если вы новичок, вы не участвуете в реальном проекте, поэтому изучите лучший фреймворк