跪求GridView控件中的RowCommand事件的使用方法(孟子等大人们进啊,再解决不了我就失业了)

angeldjd 2007-04-04 12:02:03
我在GRIDVIEW里放置了一列,在这个列里是读取数据库中IMAGEURL数据字段的,我把它绑定到了一个IMAGE控件上,这样在GRIDWIEW控件就可以显示出图片了,但我现在想还可以编辑修改,所以我在EditItemTempLatek中放置了三个控件,一个是FileUpLoad控件,用来读取图片路径,一个BUTTON控件,用来上传,还有一个TextBox控件,用来和数据库字段绑定更新,当点击上传按扭的时候,上传图片的同时,也把 TextBox控件中的图片路径改变。这样在更新的时候就应该成功了吧。现在的问题是GRIDVIEW控件中BUTTON控件的事件击要应用到 RowCommand,于是我就编写了一个
GridView1_RowCommand(object sender, GridViewCommandEventArgs e)事件,可上传的时候,还需要FileUpLoad控件中选取的路径,而在RowCommand控件中确又不能用FidControl。。。。希望高手们帮帮我啊,我想一定是有办法可以解决的吧。
...全文
1540 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzf86211861 2009-11-26
  • 打赏
  • 举报
回复
顶下 楼主
jelenyoung 2009-11-25
  • 打赏
  • 举报
回复
在gridview的rowcommand事件中取模板列中控件的值
在模板列中将button的commandname设置为deletee 并且在html中将他的CommandArgument属性和RowIndex进行绑定:

<asp:TemplateField HeaderText="产品名">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# bind("productid") %>'></asp:TextBox>
<asp:Button ID="Button3" runat="server" CommandName="deletee" Text="Button" CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>' />
</ItemTemplate>
</asp:TemplateField>

后台中输出模板列中textbox中的值(保存的产品的id):

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "deletee")
{

int i = Convert.ToInt32(e.CommandArgument.ToString());


TextBox tb = (TextBox)GridView1.Rows[i].FindControl("textbox3");

string str = tb.Text.Trim();
Response.Write(str);

}

}

liuhong_0325 2009-02-12
  • 打赏
  • 举报
回复
这个问题 RowCommand 事件要怎么获得行号了? 要获得DataKeys就要先知道行号。。
孟子E章 2007-04-04
  • 打赏
  • 举报
回复
效果如图
http://dotnet.aspx.cc/Book/DataBind1.gif
孟子E章 2007-04-04
  • 打赏
  • 举报
回复

<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>
孟子E章 2007-04-04
  • 打赏
  • 举报
回复
给你编辑修改的例子

<%@ 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>
bob_er 2007-04-04
  • 打赏
  • 举报
回复
.NET技术群12845737.
大量学习资料下载.

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

欢迎学习和技术人员加入
探讨技术,分享程序员生活.
hy_lihuan 2007-04-04
  • 打赏
  • 举报
回复
FindControl这个怎么会没有呢?应该是你自己程序没写对阿...
不过建议楼主可以换一种操作模式可能会比较简单;
losteveb 2007-04-04
  • 打赏
  • 举报
回复
顶!不能让lz失业
懒牛科技 2007-04-04
  • 打赏
  • 举报
回复
Try

你可以定义一个类级别的变量,把FileUpLoad的路径值赋给它.
然后在GridView1_RowCommand(object sender, GridViewCommandEventArgs e)直接使用这个变量不就得到上传文件的路径了吗!!
nimeide1234567890 2007-04-04
  • 打赏
  • 举报
回复
ding
angeldjd 2007-04-04
  • 打赏
  • 举报
回复
谢孟大人,哈哈。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.CompareTo("Button1") == 0)
{

string name = ((FileUpload)GridView1.Rows[GridView1.EditIndex].FindControl("FileUpLoad1")).FileName;
string size = ((FileUpload)GridView1.Rows[GridView1.EditIndex].FindControl("FileUpLoad1")).PostedFile.ContentLength.ToString();
string type = ((FileUpload)GridView1.Rows[GridView1.EditIndex].FindControl("FileUpLoad1")).PostedFile.ContentType;
string type2 = name.Substring(name.LastIndexOf(".") + 1);
string ipath = Server.MapPath("upload") + "\\" + name;

if (type2 == "jpg" || type2 == "gif")
{
((FileUpload)GridView1.Rows[GridView1.EditIndex].FindControl("FileUpLoad1")).SaveAs(ipath);
((TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("TextBox1")).Text = "./manager/upload" + "\\" + name;
}

}


}
这是我改后正确的。

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧