本文介绍了如何使用sqldatasource获取和更新gridview中具有子父关系的两个下拉列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个显示我的产品的gridview,如果我在网格视图中选择Edit,我可以编辑它们。 我的gridview中的EditItemTemplate中有两个嵌套的DropDownList与子父关系,第一个DropDownList(父)与我的产品品牌相关,第二个DropDownList(子)与我的产品类别相关。我的意思是当用户在第一个DropDownList(品牌)中选择一个Item时,Second DropDownList显示与所选品牌相关的类别列表。 在我的gridview中关联代码: I have a gridview which shows my products and I can Edit them if I choose Edit in my grid view;I have two nested DropDownList with child-parent relation in EditItemTemplate in my gridview,the first DropDownList (parent) is related to my products brand and the second DropDownList (child) is related to my products Categories. I mean when user select one Item in in first DropDownList (brands) the Second DropDownList shows the list of categories which is related to selected brand.relatede code in my gridview: <asp:TemplateField > < itemtemplate > <asp:Label ID="brandname" runat="server"Text='<%#Eval("brand_name") %>'> </itemtemplate> <edititemtemplate> <asp:DropDownList ID="brandDrop" runat="server" DataTextField="brand_name" DataValueField="id" DataSourceID="SqlDataSource4" AutoPostBack="true" > <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:mobile_storeConnectionString2 %>" SelectCommand="select [id],[brand_name] from [brands]" ></edititemtemplate> <asp:TemplateField> <itemtemplate> <asp:Label ID="catname" runat="server" Text='<%# Eval("category_name") %>'> </itemtemplate> <edititemtemplate> <asp:DropDownList ID="categoryDrop" runat="server" DataTextField="category_name" DataValueField="id" DataSourceID="SqlDataSource3" AutoPostBack="true"> <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:mobile_storeConnectionString2 %>" SelectCommand="select [category_name],[id],[idfrombrands] from [categories] where idfrombrands=@idfrombrands " > <SelectParameters> <asp:ControlParameter ControlID="brandDrop" DefaultValue="1" Name="idfrombrands" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </edititemtemplate> 这是我的gridview的sqldatasource: and this is sqldatasource for my gridview:<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mobile_storeConnectionString3 %>" SelectCommand="SELECT * FROM [AdminProductList]" UpdateCommand="UPDATE AdminProductList SET p_name=@p_name,p_price=@p_price,p_qty=@p_qty,p_Img_S=@path ,idfromCategories=@cat_id,idfrombrands=@brand_id WHERE [id]=@id"> <updateparameters> <asp:Parameter Name="path" DefaultValue="default.gif" /> <asp:Parameter Name="brand_id " Type="Int32" DefaultValue="1"/> <asp:Parameter Name="cat_id " Type="Int32" DefaultValue="1"/> </updateparameters> 我的问题是我不知道怎么弄选中的值表单下拉列表,更新我的sqldatasource1和我的数据库中的表中的数据,如果用户单击编辑但没有从下拉列表中选择任何项目(我的意思是用户没有编辑品牌和类别)然后我想存储旧的下拉列表的数据 我不知道应该在GridView1_RowDataBound和Grid中写什么View1_RowUpdating 这是我在sqldatasource1中使用它的数据库中的AdminProductList视图,我的gridview使用sqldatasource1: My problem is I don''t know how to get the selected value form dropdownlists, update my sqldatasource1 and data in tables in my database and if the user click on edit but did not select any item from dropdownlists (I mean the user did not edit brands and categories )then I want to store the old data of dropdownlistsI dont know what should I write in GridView1_RowDataBound and GridView1_RowUpdatingand this is my AdminProductList view in database that I use it in sqldatasource1 and my gridview use sqldatasource1:SELECT dbo.brands.id AS brandId, dbo.categories.id AS categoryID, dbo.brands.brand_name, dbo.categories.category_name, dbo.Products.id, dbo.Products.p_name, dbo.Products.p_price, dbo.Products.p_qty, dbo.Products.p_Img_S, dbo.Products.idfromCategories, dbo.categories.idfrombrandsFROM dbo.brands INNER JOIN dbo.categories ON dbo.brands.id = dbo.categories.idfrombrands INNER JOIN dbo.Products ON dbo.categories.id = dbo.Products.idfromCategories 这是我的GridView1_RowUpdating和GridView1_RowDataBound的代码: This is my code for GridView1_RowUpdating and GridView1_RowDataBound: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //brandDrop-------------------------------------- DropDownList list1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("brandDrop"); //Label lbl = (Label)GridView1.Rows[e.RowIndex].FindControl("br"); int NewIdFromBrand = -1; if (list1.SelectedItem.Value != null) { NewIdFromBrand = Convert.ToInt32(list1.SelectedItem.Value); SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = NewIdFromBrand.ToString(); } else { } //categoryDrop---------------------------------------- DropDownList list2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("categoryDrop"); // Label lbl2 = (Label)GridView1.Rows[e.RowIndex].FindControl("cat"); if (list2.SelectedItem.Value != null) { SqlDataSource1.UpdateParameters["cat_id"].DefaultValue = list2.SelectedValue; } else { } //Photo------------------------------------------- string photoname = System.Guid.NewGuid().ToString(); GridViewRow row = GridView1.Rows[e.RowIndex]; FileUpload fileUpload = row.FindControl("FileUploadimg") as FileUpload; Label lbl3 = (Label)GridView1.Rows[e.RowIndex].FindControl("oldImage"); //(Label)GridView1.TemplateControl.FindControl("oldImage"); if (fileUpload != null && fileUpload.HasFile) { fileUpload.SaveAs(Server.MapPath("~/P_Image") + photoname + fileUpload.FileName); // fileUpload.PostedFile.SaveAs(path + "/" + photoname +filename); SqlDataSource1.UpdateParameters["path"].DefaultValue = "~/P_Image" + photoname + fileUpload.FileName; } else { SqlDataSource1.UpdateParameters["path"].DefaultValue = lbl3.Text;//oldImage.Text; } GridView1.EditIndex = -1; GridView1.DataBind(); }//databound----------------------------------------------------------- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //check if is in edit mode if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DataRowView dRowView1 = (DataRowView)e.Row.DataItem; if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList ddlStatus = (DropDownList)e.Row.FindControl("brandDrop"); ddlStatus.SelectedValue = dRowView1[0].ToString();//dRowView1[0] gives me brandId column in my view DropDownList ddlStatus2 = (DropDownList)e.Row.FindControl("categoryDrop"); ddlStatus2.SelectedValue = dRowView1[1].ToString();//dRowView1[1] gives me categooryID column in my view } } } } 我不知道这段代码有什么问题,但是当我运行此页面时,会出现以下错误:I don''t know what is wrong with this code, but when I run this page this error is given:Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 源错误:Source Error:Line 26: NewIdFromBrand = Convert.ToInt32(list1.SelectedItem.Value);Line 27: Label1.Text = NewIdFromBrand.ToString();Line 28: SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = NewIdFromBrand.ToString();Line 29: Line 30: //SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = list1.SelectedValue; 我使用此代码在另一个页面,但有一个DropDowwnList,它工作正常。 如果有人帮助我,我会非常感谢I used this code in another page but with one DropDowwnList and it works fine.if anyone help me I will be so thankfull推荐答案 这是我的gridview的sqldatasource: and this is sqldatasource for my gridview:<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<% My problem is I don’’t know how to get the selected value form dropdownlists, update my sqldatasource1 and data in tables in my database and if the user click on edit but did not select any item from dropdownlists (I mean the user did not edit brands and categories )then I want to store the old data of dropdownlists I dont know what should I write in GridView1_RowDataBound and GridView1_RowUpdating and this is my AdminProductList view in database that I use it in sqldatasource1 and my gridview use sqldatasource1: My problem is I don''t know how to get the selected value form dropdownlists, update my sqldatasource1 and data in tables in my database and if the user click on edit but did not select any item from dropdownlists (I mean the user did not edit brands and categories )then I want to store the old data of dropdownlistsI dont know what should I write in GridView1_RowDataBound and GridView1_RowUpdatingand this is my AdminProductList view in database that I use it in sqldatasource1 and my gridview use sqldatasource1:SELECT dbo.brands.id AS brandId, dbo.categories.id AS categoryID, dbo.brands.brand_name, dbo.categories.category_name, dbo.Products.id, dbo.Products.p_name, dbo.Products.p_price, dbo.Products.p_qty, dbo.Products.p_Img_S, dbo.Products.idfromCategories, dbo.categories.idfrombrandsFROM dbo.brands INNER JOIN dbo.categories ON dbo.brands.id = dbo.categories.idfrombrands INNER JOIN dbo.Products ON dbo.categories.id = dbo.Products.idfromCategories This is my code for GridView1_RowUpdating and GridView1_RowDataBound: This is my code for GridView1_RowUpdating and GridView1_RowDataBound: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //brandDrop-------------------------------------- DropDownList list1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("brandDrop"); //Label lbl = (Label)GridView1.Rows[e.RowIndex].FindControl("br"); int NewIdFromBrand = -1; if (list1.SelectedItem.Value != null) { NewIdFromBrand = Convert.ToInt32(list1.SelectedItem.Value); SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = NewIdFromBrand.ToString(); } else { } //categoryDrop---------------------------------------- DropDownList list2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("categoryDrop"); // Label lbl2 = (Label)GridView1.Rows[e.RowIndex].FindControl("cat"); if (list2.SelectedItem.Value != null) { SqlDataSource1.UpdateParameters["cat_id"].DefaultValue = list2.SelectedValue; } else { } //Photo------------------------------------------- string photoname = System.Guid.NewGuid().ToString(); GridViewRow row = GridView1.Rows[e.RowIndex]; FileUpload fileUpload = row.FindControl("FileUploadimg") as FileUpload; Label lbl3 = (Label)GridView1.Rows[e.RowIndex].FindControl("oldImage"); //(Label)GridView1.TemplateControl.FindControl("oldImage"); if (fileUpload != null && fileUpload.HasFile) { fileUpload.SaveAs(Server.MapPath("~/P_Image") + photoname + fileUpload.FileName); // fileUpload.PostedFile.SaveAs(path + "/" + photoname +filename); SqlDataSource1.UpdateParameters["path"].DefaultValue = "~/P_Image" + photoname + fileUpload.FileName; } else { SqlDataSource1.UpdateParameters["path"].DefaultValue = lbl3.Text;//oldImage.Text; } GridView1.EditIndex = -1; GridView1.DataBind(); }//databound----------------------------------------------------------- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //check if is in edit mode if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DataRowView dRowView1 = (DataRowView)e.Row.DataItem; if (e.Row.RowType == DataControlRowType.DataRow) { if ((e.Row.RowState & DataControlRowState.Edit) > 0) { DropDownList ddlStatus = (DropDownList)e.Row.FindControl("brandDrop"); ddlStatus.SelectedValue = dRowView1[0].ToString();//dRowView1[0] gives me brandId column in my view DropDownList ddlStatus2 = (DropDownList)e.Row.FindControl("categoryDrop"); ddlStatus2.SelectedValue = dRowView1[1].ToString();//dRowView1[1] gives me categooryID column in my view } } } } I don’’t know what is wrong with this code, but when I run this page this error is given:I don''t know what is wrong with this code, but when I run this page this error is given:Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error:Source Error:Line 26: NewIdFromBrand = Convert.ToInt32(list1.SelectedItem.Value);Line 27: Label1.Text = NewIdFromBrand.ToString();Line 28: SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = NewIdFromBrand.ToString();Line 29: Line 30: //SqlDataSource1.UpdateParameters["brand_id"].DefaultValue = list1.SelectedValue; I used this code in another page but with one DropDowwnList and it works fine. if anyone help me I will be so thankfullI used this code in another page but with one DropDowwnList and it works fine.if anyone help me I will be so thankfull 这篇关于如何使用sqldatasource获取和更新gridview中具有子父关系的两个下拉列表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 18:07