读书人

跪求GridView控件中的RowCommand事件的

发布时间: 2011-12-19 23:23:36 作者: rapoo

跪求GridView控件中的RowCommand事件的使用方法(孟子等大人们进啊,再解决不了我就失业了)
我在GRIDVIEW里放置了一列,在这个列里是读取数据库中IMAGEURL数据字段的,我把它绑定到了一个IMAGE控件上,这样在GRIDWIEW控件就可以显示出图片了,但我现在想还可以编辑修改,所以我在EditItemTempLatek中放置了三个控件,一个是FileUpLoad控件,用来读取图片路径,一个BUTTON控件,用来上传,还有一个TextBox控件,用来和数据库字段绑定更新,当点击上传按扭的时候,上传图片的同时,也把 TextBox控件中的图片路径改变。这样在更新的时候就应该成功了吧。现在的问题是GRIDVIEW控件中BUTTON控件的事件击要应用到 RowCommand,于是我就编写了一个
GridView1_RowCommand(object sender, GridViewCommandEventArgs e)事件,可上传的时候,还需要FileUpLoad控件中选取的路径,而在RowCommand控件中确又不能用FidControl。。。。希望高手们帮帮我啊,我想一定是有办法可以解决的吧。


[解决办法]
ding
[解决办法]
Try

你可以定义一个类级别的变量,把FileUpLoad的路径值赋给它.
然后在GridView1_RowCommand(object sender, GridViewCommandEventArgs e)直接使用这个变量不就得到上传文件的路径了吗!!
[解决办法]
顶!不能让lz失业
[解决办法]
FindControl这个怎么会没有呢?应该是你自己程序没写对阿...
不过建议楼主可以换一种操作模式可能会比较简单;
[解决办法]
.NET技术群12845737.
大量学习资料下载.

讨论VC/C#/ASP.NET/FLASH_AS技术

欢迎学习和技术人员加入
探讨技术,分享程序员生活.
[解决办法]
给你编辑修改的例子

<%@ Page Language= "C# " AutoEventWireup= "true " Debug= "true " %>
<%@ Import Namespace= "System.Data " %>
<script runat= "server ">
string GetUserPhoto(object pathPhoto)
{
if (pathPhoto == DBNull.Value)
{
return " <img src= '../Images/none.gif '> ";
}
else
{
return " <img src= '../Upload/ " + pathPhoto.ToString() + " '> ";
}
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == GridView1.EditIndex)
{

DataRowView rowItem = (DataRowView)e.Row.DataItem;

DropDownList clsName = (DropDownList)e.Row.FindControl( "uClassName ");
if (rowItem[ "ClassName "] != DBNull.Value)
{
clsName.Items.FindByText(rowItem[ "ClassName "].ToString()).Selected = true;
}

if (rowItem[ "Gender "] != DBNull.Value)
{

RadioButtonList oGender = (RadioButtonList)e.Row.FindControl( "uGender ");
oGender.SelectedIndex = (Convert.ToBoolean(rowItem[ "Gender "]) ? 0 : 1);
}
}
}
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "New ")
{
string StudentTitle = ((TextBox)GridView1.FooterRow.FindControl( "NewTitle ")).Text;
if (StudentTitle.Trim() == " ")
{
ErrorMsg.Text = "请输入姓名 ";
return;
}
string StudentBirthDay = ((TextBox)GridView1.FooterRow.FindControl( "NewBirthDay ")).Text;
bool StudentGender = ((RadioButtonList)
GridView1.FooterRow.FindControl( "NewGender ")).SelectedValue == "男 " ? true : false;
string StudentClassName = ((DropDownList)
GridView1.FooterRow.FindControl( "NewClassName ")).SelectedValue;
FileUpload oUpload = (FileUpload)GridView1.FooterRow.FindControl( "AddPhoto ");
String FileName = " ";
FileName = Guid.NewGuid().ToString( "D ") + System.IO.Path.GetExtension(oUpload.FileName);


oUpload.SaveAs(Server.MapPath( "~ ") + "/Upload/ " + FileName);
string sql = "Insert Into Student (Title,BirthDay,Gender,PhotoPath,ClassName) ";
sql += " Values(@Title,@BirthDay,@Gender,@PhotoPath,@ClassName) ";
SqlDataSource1.InsertCommand = sql;
SqlDataSource1.InsertParameters.Add( "@Title ", TypeCode.String, StudentTitle);
SqlDataSource1.InsertParameters.Add( "@BirthDay ", TypeCode.DateTime, StudentBirthDay);
SqlDataSource1.InsertParameters.Add( "@Gender ", TypeCode.Boolean, StudentGender.ToString());
SqlDataSource1.InsertParameters.Add( "@PhotoPath ", TypeCode.String, FileName);
SqlDataSource1.InsertParameters.Add( "@ClassName ", TypeCode.String, StudentClassName);
SqlDataSource1.Insert();
}

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 = " ";
bool HasFileUploaded = false;

FileUpload oUpload = (FileUpload)GridView1.Rows[GridView1.EditIndex].FindControl( "uPhoto ");
if (oUpload.HasFile)
{
PhotoPath = Guid.NewGuid().ToString( "D ") + System.IO.Path.GetExtension(oUpload.FileName);
oUpload.SaveAs(Server.MapPath( "~ ") + "/Upload/ " + PhotoPath);
HasFileUploaded = true;
}

if (HasFileUploaded)
{
sql = "Update Student Set Title=@Title,BirthDay = @BirthDay, ";
sql += "Gender=@Gender,PhotoPath=@PhotoPath,ClassName=@ClassName Where id=@id ";
}
else
{
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();
}
}
</script>

<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> GridView 插入、删除、修改的例子 </title>
</head>
[解决办法]

<body>
<form id= "form1 " runat= "server ">
<asp:GridView ID= "GridView1 " runat= "server " AutoGenerateColumns= "False " DataKeyNames= "id "


AllowPaging= "true " AllowSorting= "true " PageSize= "5 " DataSourceID= "SqlDataSource1 "
OnRowDataBound= "GridView1_RowDataBound " ShowFooter= "true "
OnRowCommand= "GridView1_RowCommand ">
<Columns>
<asp:BoundField DataField= "id " HeaderText= "id " InsertVisible= "False " ReadOnly= "True "
Visible= "false " SortExpression= "id " />
<asp:TemplateField HeaderText= "姓名 " SortExpression= "Title ">
<ItemTemplate>
<%#Eval( "Title ") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID= "uTitle " runat= "server " Text= ' <%#Eval( "Title ") %> '> </asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID= "NewTitle " runat= "server "> </asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "生日 " SortExpression= "BirthDay ">
<ItemTemplate>
<%#Eval( "BirthDay ", "{0:yyyy年M月d日} ")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID= "uBirthDay " runat= "server " Text= ' <%#Eval( "BirthDay ", "{0:yyyy-M-d} ")%> '/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID= "NewBirthDay " runat= "server "> </asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "性别 " SortExpression= "Gender ">
<ItemTemplate>
<%#(Eval( "Gender ")).ToString() == "True "? "男 ": "女 "%>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID= "g " Text= ' <%#(Eval( "Gender ")).ToString() == "True "? "男 ": "女 "%> '
Visible= "false " runat= "server "> </asp:Label>
<asp:RadioButtonList ID= "uGender " runat= "server ">
<asp:ListItem Text= "男 "> </asp:ListItem>
<asp:ListItem Text= "女 "> </asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID= "NewGender " runat= "server ">
<asp:ListItem Selected= "true " Text= "男 "> </asp:ListItem>
<asp:ListItem Text= "女 "> </asp:ListItem>
</asp:RadioButtonList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText= "年级 ">
<ItemTemplate>
<%#Eval( "ClassName ")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID= "uClassName " runat= "server ">
<asp:ListItem Text= "小学 " Value= "小学 "> </asp:ListItem>
<asp:ListItem Text= "中学 " Value= "中学 "> </asp:ListItem>
<asp:ListItem Text= "高中 " Value= "高中 "> </asp:ListItem>


<asp:ListItem Text= "大学 " Value= "大学 "> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID= "NewClassName " runat= "server ">
<asp:ListItem Text= "小学 " Value= "小学 "> </asp:ListItem>
<asp:ListItem Text= "中学 " Value= "中学 "> </asp:ListItem>
<asp:ListItem Text= "高中 " Value= "高中 "> </asp:ListItem>
<asp:ListItem Text= "大学 " Value= "大学 "> </asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# GetUserPhoto(Eval( "PhotoPath ")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID= "uPhoto " runat= "server " />
</EditItemTemplate>
<FooterTemplate>
<asp:FileUpload ID= "AddPhoto " runat= "server " />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType= "Button " ShowCancelButton= "true " ShowDeleteButton= "true "
ShowEditButton= "true " CancelText= "取消 " DeleteText= "删除 " UpdateText= "更新 "
EditText= "修改 " HeaderText= "操作 " InsertVisible= "false "
ShowInsertButton= "true " NewText= "添加学生 " />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ProviderName= "System.Data.OleDb "
ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ASPNET20Book.mdb; "
SelectCommand= "SELECT * FROM [Student] ORDER BY [id] DESC " DataSourceMode= "DataSet "
DeleteCommand= "Delete from Student Where id=@id ">
<DeleteParameters>
<asp:Parameter Name= "id " Type= "Int32 " />
</DeleteParameters>
</asp:SqlDataSource>
<asp:Label ID= "ErrorMsg " runat= "server " ForeColor= "red "> </asp:Label>
</form>
</body>
</html>

读书人网 >asp.net

热点推荐