首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

让小弟我头痛的GridView Template DropdownList ObjectDataSource.强汗无法传参数

2011-12-31 
让我头痛的GridView Template DropdownListObjectDataSource...强汗无法传参数我想把GridView里的Dropdown

让我头痛的GridView Template DropdownList ObjectDataSource...强汗无法传参数
我想把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方法?

[解决办法]
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{


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();
}
}

热点排行