我想把GridView 里的 DropdownList 选择的值,做为参数,传给UPdate操作方法,可是每次更改都传不过去.请高人指..
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="ProductDB" SelectMethod="GetProduct" UpdateMethod="UpdateProduct" DeleteMethod="DeleteProduct" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="old_{0}" OnDeleted="ObjectDataSource1_Deleted" OnUpdated="ObjectDataSource1_Updated">
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="InPrice" Type="String" />
<asp:Parameter Name="ProductCount" Type="String" />
<asp:ControlParameter Name="ProductTypeID" Type="Int32" ControlID="GridView1.ProductTypeIDtt" PropertyName="SelectedValue" />
<asp:Parameter Name="old_ProductID" Type="Int32" />
<asp:Parameter Name="old_ProductName" Type="String" />
<asp:Parameter Name="old_InPrice" Type="String" />
<asp:Parameter Name="old_ProductCount" Type="String" />
<asp:ControlParameter Name="old_ProductTypeID" Type="Int32" ControlID="GridView1.ProductTypeIDtt" PropertyName="SelectedValue" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="old_ProductID" Type="Int32" />
<asp:Parameter Name="old_ProductName" Type="String" />
<asp:Parameter Name="old_InPrice" Type="String" />
<asp:Parameter Name="old_ProductCount" Type="String" />
</DeleteParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" DataKeyNames="ProductID" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" Visible="false" />
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:TemplateField HeaderText ="Product Type">
<ItemTemplate>
<asp:Label ID="lblProductType" runat ="server" Text='<%# Eval("ProductTypeName") %>'></asp:Label><asp:HiddenField ID="HidValue" Value='<%# Eval("ProductTypeID") %>' runat ="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ProductTypeIDtt" runat="server" DataTextField="ProductTypeName" DataValueField="ProductTypeID" DataSource='<%# GetProductTypeName() %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="InPrice" HeaderText="In Price" />
<asp:BoundField DataField="ProductCount" HeaderText="Product Count" />
</Columns>
</asp:GridView>
</div>
</form>后台CS如下
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// ProductDB 的摘要说明
/// </summary>
public class ProductDB
{
public ProductDB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
string _strConString = ConfigurationManager.ConnectionStrings["NetStudyConnectionString"].ConnectionString; public SqlDataReader GetProduct()
{
SqlConnection con = new SqlConnection(_strConString);
string selectString = "select * from Product Left join ProductType on ProductTypeID = GroupID";
SqlCommand cmd = new SqlCommand(selectString, con);
con.Open();
SqlDataReader dtr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dtr;
} public int UpdateProduct(string ProductName, string InPrice, string ProductCount, int ProductTypeID,int old_ProductID, string old_ProductName, string old_InPrice, string old_ProductCount, int old_ProductTypeID)
{
string Time = DateTime.Now.ToLongTimeString();
SqlConnection con = new SqlConnection(_strConString);
string updateString = "UPDATE Product Set ProductName=@ProductName,GroupID=@GroupID,InPrice=@InPrice,ProductCount=@ProductCount";
updateString += ",UpdateTime=@UpdateTime WHERE ProductID=@old_ProductID And ProductName=@old_ProductName And InPrice=@old_InPrice And ProductCount=@old_ProductCount And GroupID=@old_GroupID";
SqlCommand cmd = new SqlCommand(updateString, con);
cmd.Parameters.AddWithValue("@ProductName", ProductName);
cmd.Parameters.AddWithValue("@InPrice", InPrice);
cmd.Parameters.AddWithValue("@ProductCount", ProductCount);
cmd.Parameters.AddWithValue("@UpdateTime", Time);
cmd.Parameters.AddWithValue("@old_ProductID", old_ProductID);
cmd.Parameters.AddWithValue("@old_ProductName", old_ProductName);
cmd.Parameters.AddWithValue("@old_InPrice", old_InPrice);
cmd.Parameters.AddWithValue("@old_ProductCount", old_ProductCount);
cmd.Parameters.AddWithValue("@GroupID", ProductTypeID);
cmd.Parameters.AddWithValue("@old_GroupID", old_ProductTypeID);
int returnCounter = 0;
con.Open();
returnCounter = cmd.ExecuteNonQuery();
con.Close();
return returnCounter;
} public int DeleteProduct(int old_ProductID,string old_ProductName,string old_InPrice,string old_ProductCount)
{
SqlConnection con = new SqlConnection(_strConString);
string deleteString = "Delete From Product WHERE ProductID=@old_ProductID And ProductName=@old_ProductName And InPrice=@old_InPrice And ProductCount=@old_ProductCount";
SqlCommand cmd = new SqlCommand(deleteString, con);
cmd.Parameters.AddWithValue("@old_ProductID", old_ProductID);
cmd.Parameters.AddWithValue("@old_ProductName", old_ProductName);
cmd.Parameters.AddWithValue("@old_InPrice", old_InPrice);
cmd.Parameters.AddWithValue("@old_ProductCount", old_ProductCount);
int delCounter = 0;
con.Open();
delCounter = cmd.ExecuteNonQuery();
con.Close();
return delCounter;
}
}
只有是BoundField 列的值才可以修改.在GRIDVIEW 里的DropdownList 的 selectvalue 的值传给UpdateProduct方法?
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="ProductDB" SelectMethod="GetProduct" UpdateMethod="UpdateProduct" DeleteMethod="DeleteProduct" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="old_{0}" OnDeleted="ObjectDataSource1_Deleted" OnUpdated="ObjectDataSource1_Updated">
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="InPrice" Type="String" />
<asp:Parameter Name="ProductCount" Type="String" />
<asp:ControlParameter Name="ProductTypeID" Type="Int32" ControlID="GridView1.ProductTypeIDtt" PropertyName="SelectedValue" />
<asp:Parameter Name="old_ProductID" Type="Int32" />
<asp:Parameter Name="old_ProductName" Type="String" />
<asp:Parameter Name="old_InPrice" Type="String" />
<asp:Parameter Name="old_ProductCount" Type="String" />
<asp:ControlParameter Name="old_ProductTypeID" Type="Int32" ControlID="GridView1.ProductTypeIDtt" PropertyName="SelectedValue" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="old_ProductID" Type="Int32" />
<asp:Parameter Name="old_ProductName" Type="String" />
<asp:Parameter Name="old_InPrice" Type="String" />
<asp:Parameter Name="old_ProductCount" Type="String" />
</DeleteParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" DataKeyNames="ProductID" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" Visible="false" />
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:TemplateField HeaderText ="Product Type">
<ItemTemplate>
<asp:Label ID="lblProductType" runat ="server" Text='<%# Eval("ProductTypeName") %>'></asp:Label><asp:HiddenField ID="HidValue" Value='<%# Eval("ProductTypeID") %>' runat ="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ProductTypeIDtt" runat="server" DataTextField="ProductTypeName" DataValueField="ProductTypeID" DataSource='<%# GetProductTypeName() %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="InPrice" HeaderText="In Price" />
<asp:BoundField DataField="ProductCount" HeaderText="Product Count" />
</Columns>
</asp:GridView>
</div>
</form>后台CS如下
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// ProductDB 的摘要说明
/// </summary>
public class ProductDB
{
public ProductDB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
string _strConString = ConfigurationManager.ConnectionStrings["NetStudyConnectionString"].ConnectionString; public SqlDataReader GetProduct()
{
SqlConnection con = new SqlConnection(_strConString);
string selectString = "select * from Product Left join ProductType on ProductTypeID = GroupID";
SqlCommand cmd = new SqlCommand(selectString, con);
con.Open();
SqlDataReader dtr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dtr;
} public int UpdateProduct(string ProductName, string InPrice, string ProductCount, int ProductTypeID,int old_ProductID, string old_ProductName, string old_InPrice, string old_ProductCount, int old_ProductTypeID)
{
string Time = DateTime.Now.ToLongTimeString();
SqlConnection con = new SqlConnection(_strConString);
string updateString = "UPDATE Product Set ProductName=@ProductName,GroupID=@GroupID,InPrice=@InPrice,ProductCount=@ProductCount";
updateString += ",UpdateTime=@UpdateTime WHERE ProductID=@old_ProductID And ProductName=@old_ProductName And InPrice=@old_InPrice And ProductCount=@old_ProductCount And GroupID=@old_GroupID";
SqlCommand cmd = new SqlCommand(updateString, con);
cmd.Parameters.AddWithValue("@ProductName", ProductName);
cmd.Parameters.AddWithValue("@InPrice", InPrice);
cmd.Parameters.AddWithValue("@ProductCount", ProductCount);
cmd.Parameters.AddWithValue("@UpdateTime", Time);
cmd.Parameters.AddWithValue("@old_ProductID", old_ProductID);
cmd.Parameters.AddWithValue("@old_ProductName", old_ProductName);
cmd.Parameters.AddWithValue("@old_InPrice", old_InPrice);
cmd.Parameters.AddWithValue("@old_ProductCount", old_ProductCount);
cmd.Parameters.AddWithValue("@GroupID", ProductTypeID);
cmd.Parameters.AddWithValue("@old_GroupID", old_ProductTypeID);
int returnCounter = 0;
con.Open();
returnCounter = cmd.ExecuteNonQuery();
con.Close();
return returnCounter;
} public int DeleteProduct(int old_ProductID,string old_ProductName,string old_InPrice,string old_ProductCount)
{
SqlConnection con = new SqlConnection(_strConString);
string deleteString = "Delete From Product WHERE ProductID=@old_ProductID And ProductName=@old_ProductName And InPrice=@old_InPrice And ProductCount=@old_ProductCount";
SqlCommand cmd = new SqlCommand(deleteString, con);
cmd.Parameters.AddWithValue("@old_ProductID", old_ProductID);
cmd.Parameters.AddWithValue("@old_ProductName", old_ProductName);
cmd.Parameters.AddWithValue("@old_InPrice", old_InPrice);
cmd.Parameters.AddWithValue("@old_ProductCount", old_ProductCount);
int delCounter = 0;
con.Open();
delCounter = cmd.ExecuteNonQuery();
con.Close();
return delCounter;
}
}
只有是BoundField 列的值才可以修改.在GRIDVIEW 里的DropdownList 的 selectvalue 的值传给UpdateProduct方法?
解决方案 »
- 关于XML问题
- 如何使一打开登录页面就没有地址栏?如何在退出登录时能直接转到login.aspx?
- 使用ajax时如何在cs中调用js函数?
- 求教iis6.0如何设置允许一段ip访问?急啊
- 如何解决在提交时,输入了<form....
- 怪哉!怪哉!为什么我的GridView.Rows[1].Cell[1].Text读不出任何值来??
- GDI++画图问题
- 求[[[[[[ComponentArt TreeView for ASP.NET V1.5 源代码]]]]]----------------------UP的有分
- 高分跪求一个好的分页控件!
- asp.net如何连接sql数据库????????
- 打开网上的msdn时,就会弹出【Visual Studio实时调试器】,若不调试,所有的网页将关闭
- 第一次使用VS2005请大家给个思路!
{
if (e.CommandName == "Update")
{
string StudentTitle = ((TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("uTitle")).Text;
string StudentBirthDay = ((TextBox)
GridView1.Rows[GridView1.EditIndex].FindControl("uBirthDay")).Text;
bool StudentGender = ((RadioButtonList)
GridView1.Rows[GridView1.EditIndex].FindControl("uGender")).SelectedValue == "男" ? true : false;
string StudentClassName = ((DropDownList)
GridView1.Rows[GridView1.EditIndex].FindControl("uClassName")).SelectedValue;
string StudentID = GridView1.DataKeys[GridView1.EditIndex].Value.ToString();
String FileName = "";
string sql = "";
String PhotoPath = ""; sql = "Update Student Set Title=@Title,BirthDay = @BirthDay," ;
sql += "Gender=@Gender,ClassName=@ClassName Where id=@id";
SqlDataSource1.UpdateCommand = sql;
SqlDataSource1.UpdateCommandType = SqlDataSourceCommandType.Text; SqlDataSource1.UpdateParameters.Add("@Title", TypeCode.String, StudentTitle);
SqlDataSource1.UpdateParameters.Add("@BirthDay", TypeCode.DateTime, StudentBirthDay);
SqlDataSource1.UpdateParameters.Add("@Gender", TypeCode.Boolean, StudentGender.ToString());
if (HasFileUploaded)
{
SqlDataSource1.UpdateParameters.Add("@PhotoPath", TypeCode.String, PhotoPath);
}
SqlDataSource1.UpdateParameters.Add("@ClassName", TypeCode.String, StudentClassName);
SqlDataSource1.UpdateParameters.Add("@id", TypeCode.Int32, StudentID); SqlDataSource1.Update();
}
}
ObjectDataSource“ObjectDataSource1”未能找到带参数的非泛型方法“UpdateProduct”: ProductName, InPrice, ProductCount, old_ProductID, old_ProductName, old_InPrice, old_ProductCount, ProductTypeID。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: ObjectDataSource“ObjectDataSource1”未能找到带参数的非泛型方法“UpdateProduct”: ProductName, InPrice, ProductCount, old_ProductID, old_ProductName, old_InPrice, old_ProductCount, ProductTypeID。源错误:
现在代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductList.aspx.cs" Inherits="ProductList" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="ProductDB" SelectMethod="GetProduct" UpdateMethod="UpdateProduct" DeleteMethod="DeleteProduct" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="old_{0}" OnDeleted="ObjectDataSource1_Deleted" OnUpdated="ObjectDataSource1_Updated">
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="InPrice" Type="String" />
<asp:Parameter Name="ProductCount" Type="String" />
<asp:Parameter Name="old_ProductID" Type="Int32" />
<asp:Parameter Name="old_ProductName" Type="String" />
<asp:Parameter Name="old_InPrice" Type="String" />
<asp:Parameter Name="old_ProductCount" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="old_ProductID" Type="Int32" />
<asp:Parameter Name="old_ProductName" Type="String" />
<asp:Parameter Name="old_InPrice" Type="String" />
<asp:Parameter Name="old_ProductCount" Type="String" />
</DeleteParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" DataKeyNames="ProductID" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" Visible="false" />
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:TemplateField HeaderText ="Product Type">
<ItemTemplate>
<asp:Label ID="lblProductType" runat ="server" Text='<%# Eval("ProductTypeName") %>'></asp:Label><asp:HiddenField ID="HidValue" Value='<%# Eval("ProductTypeID") %>' runat ="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ProductTypeIDtt" runat="server" DataTextField="ProductTypeName" DataValueField="ProductTypeID" DataSource='<%# GetProductTypeName() %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="InPrice" HeaderText="In Price" />
<asp:BoundField DataField="ProductCount" HeaderText="Product Count" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
<tr>
<td align="center" valign="top">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" DataSourceID="ObjectDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="选择" SortExpression="Fieldid">
<ItemTemplate>
<%# ShowCheckBox(DataBinder.Eval(Container.DataItem, "Fieldid").ToString()) %>
</ItemTemplate>
<ItemStyle Width="35px" />
<HeaderStyle Font-Size="Small" />
</asp:TemplateField>
<asp:BoundField HeaderText="说明" SortExpression="FieldText" DataField="FieldText">
<HeaderStyle Font-Size="Small" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
<asp:BoundField HeaderText="名称" SortExpression="FieldName" DataField="FieldName">
<HeaderStyle Font-Size="Small" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
<asp:BoundField HeaderText="元素类型" SortExpression="ItemName" DataField="ItemName">
<HeaderStyle Font-Size="Small" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
*****该死的在这里,如果全部是BoundField类型修改没问题,但改成DropDownList就说找不到@FieldType(数据类型)参数呢?
<asp:TemplateField HeaderText="数据类型" SortExpression="FieldType">
<EditItemTemplate>
<asp:DropDownList ID="FieldType" runat="server" AppendDataBoundItems="true" SelectedValue='<%# DataBinder.Eval(Container.DataItem,"FieldType") %>'>
<asp:ListItem Value="bigint" Text="bigint"></asp:ListItem>
<asp:ListItem Value="int" Text="int"></asp:ListItem>
<asp:ListItem Value="tinyint" Text="tinyint"></asp:ListItem>
<asp:ListItem Value="bit" Text="bit"></asp:ListItem>
<asp:ListItem Value="varchar" Text="varchar"></asp:ListItem>
<asp:ListItem Value="datetime" Text="datetime"></asp:ListItem>
<asp:ListItem Value="float" Text="float"></asp:ListItem>
<asp:ListItem Value="text" Text="text"></asp:ListItem>
<asp:ListItem Value="" Text="(None)"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFieldType" runat="server" Text='<%# Bind("FieldType") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Font-Size="Small" />
<ItemStyle Font-Size="Small" />
</asp:TemplateField>
<asp:BoundField HeaderText="数据长度" SortExpression="FieldLength" DataField="FieldLength">
<HeaderStyle Font-Size="Small" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
<asp:CheckBoxField HeaderText="是否允空" SortExpression="FieldIsNull" DataField="FieldIsNull">
<HeaderStyle Font-Size="Small" />
</asp:CheckBoxField>
<asp:CheckBoxField HeaderText="是否验证" SortExpression="FieldIsValidate" DataField="FieldIsValidate">
<HeaderStyle Font-Size="Small" />
</asp:CheckBoxField>
<asp:BoundField HeaderText="宽度" SortExpression="FieldWidth" DataField="FieldWidth">
<HeaderStyle Font-Size="Small" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
<asp:BoundField HeaderText="高度" SortExpression="FieldHeight" DataField="FieldHeight">
<HeaderStyle Font-Size="Small" />
<ItemStyle Font-Size="Small" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="True">
<HeaderStyle Font-Size="9pt" />
<ItemStyle Width="80px" />
</asp:CommandField>
</Columns>
<PagerSettings Mode="NumericFirstLast" />
<EmptyDataTemplate>
<div style=" width:100%; height:400px; text-align:center; vertical-align:middle;"><br />没有相关信息</div>
</EmptyDataTemplate>
<PagerStyle Font-Size="12pt" Height="28px" HorizontalAlign="Right" VerticalAlign="Middle" />
<HeaderStyle BackColor="SteelBlue" BorderColor="#8080FF" BorderStyle="Groove" Font-Bold="True"
Font-Size="13pt" ForeColor="White" Height="25px" HorizontalAlign="Center" VerticalAlign="Middle" />
<AlternatingRowStyle BackColor="LightBlue" BorderColor="MediumSeaGreen" />
<EditRowStyle CssClass="InputCss" Width="100px" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server">
<DeleteParameters>
<asp:Parameter Name="Fieldid" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Fieldid" Type="Int32" />
<asp:Parameter Name="FieldText" Type="String" />
<asp:Parameter Name="FieldName" Type="String" />
<asp:Parameter Name="ItemName" Type="Int32" />
<asp:Parameter Name="FieldType" Type="String" />
<asp:Parameter Name="FieldLength" Type="Int32" />
<asp:Parameter Name="FieldIsNull" Type="Boolean" />
<asp:Parameter Name="FieldIsValidate" Type="Boolean" />
<asp:Parameter Name="FieldWidth" Type="Int32" />
<asp:Parameter Name="FieldHeight" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
</td>
</tr>
</table>
</form>
</body>
</html>