我用GridView绑定了数据。我想要做编辑更新功能。
<asp:TemplateField HeaderText="对与错">
<EditItemTemplate>
<asp:RadioButton ID="rbtnYes" runat="server" Text="√" GroupName="button" />
<asp:RadioButton ID="rbtnNo" runat="server" Text="×" GroupName="button" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("Hj_YesOrNo")%>
</ItemTemplate>
</asp:TemplateField>RadioButton一进来的时候是根据数据库的值先绑定的。也就是说。数据库要是√。那rbtnYes应该默认选中。。我怎么获取EditItemTemplate里面的控件。然后进行绑定呀?我用for能只能拿出ItemTemplate里面的控件。因为RadioButton 放在EditItemTemplate中。所以不知道怎么拿if (e.Row.RowState == DataControlRowState.Edit)
{
Label lblID = e.Row.FindControl("lblID") as Label;
RadioButton rbtnYes = e.Row.FindControl("rbtnYes") as RadioButton;
RadioButton rbtnNo = e.Row.FindControl("rbtnNo") as RadioButton;
HappyReadJudge Judge = HappyReadJudgeBLL.GetJudgeByID(Convert.ToInt32(lblID.Text));
if (Judge.Hj_YesOrNo == "√")
{
rbtnYes.Checked = true;
}
else
{
rbtnNo.Checked = true;
}
}
我用了RowDataBound事件拿出数据。。可以。。但是在交替行的时候。就无法绑定了。也就是说。点第一行的时候。会进来绑定数据。但是点第二行的时候。因为是交替行。所以就不会进来。既然写了交替行。里面找出的控件也是为null 的有什么办法能把EditItemTemplate里面的控件拿出来啊?在RowUpdating里面就可以拿。但是在RowUpdating事件外怎么拿呢?会的来帮一下!!
<asp:TemplateField HeaderText="对与错">
<EditItemTemplate>
<asp:RadioButton ID="rbtnYes" runat="server" Text="√" GroupName="button" />
<asp:RadioButton ID="rbtnNo" runat="server" Text="×" GroupName="button" />
</EditItemTemplate>
<ItemTemplate>
<%# Eval("Hj_YesOrNo")%>
</ItemTemplate>
</asp:TemplateField>RadioButton一进来的时候是根据数据库的值先绑定的。也就是说。数据库要是√。那rbtnYes应该默认选中。。我怎么获取EditItemTemplate里面的控件。然后进行绑定呀?我用for能只能拿出ItemTemplate里面的控件。因为RadioButton 放在EditItemTemplate中。所以不知道怎么拿if (e.Row.RowState == DataControlRowState.Edit)
{
Label lblID = e.Row.FindControl("lblID") as Label;
RadioButton rbtnYes = e.Row.FindControl("rbtnYes") as RadioButton;
RadioButton rbtnNo = e.Row.FindControl("rbtnNo") as RadioButton;
HappyReadJudge Judge = HappyReadJudgeBLL.GetJudgeByID(Convert.ToInt32(lblID.Text));
if (Judge.Hj_YesOrNo == "√")
{
rbtnYes.Checked = true;
}
else
{
rbtnNo.Checked = true;
}
}
我用了RowDataBound事件拿出数据。。可以。。但是在交替行的时候。就无法绑定了。也就是说。点第一行的时候。会进来绑定数据。但是点第二行的时候。因为是交替行。所以就不会进来。既然写了交替行。里面找出的控件也是为null 的有什么办法能把EditItemTemplate里面的控件拿出来啊?在RowUpdating里面就可以拿。但是在RowUpdating事件外怎么拿呢?会的来帮一下!!
解决方案 »
- 高手|sql参数删除一组数据的问题
- 如何找到一下控件
- 推荐 ASP.NET 2.0 开源项目 → 多用户博客程序(i-space)
- 我安装完VS.net后,打开以前的项目文件,选新建新项的时候,显示“VC软件包不可用或未注册”,怎么办
- 请问如何用代码实现:在触发某个事件后,TextBox的显示形式才会变成密码的显示形式而不是静态显示为密码形式?谢谢!
- ******利用ASP.NET访问共享网络资源的问题???
- 求sql代码,有关count和group by的问题,呼唤高手~~~~~~~~~~~~~~~~~~~~~~~~狂谢
- 省市下拉联动,数据用options.add从js文件调,提示超出索引超出范围
- 如何用DATAGRID实现以下表头??
- 请叫各位大虾,在.aspx中如何调用javascript,不胜感激!
- 急急急急,fckeditor不可以上传图片???
- 如何实现,把excel的数据通过asp.net网页导入到SqlServer,还能通过sqlServer显示在页面上?
<asp:RadioButton ID="rbtnYes" runat="server" Text="√" GroupName="button" />
<asp:RadioButton ID="rbtnNo" runat="server" Text="×" GroupName="button" />
</EditItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="rbtnYes2" runat="server" Text="√" GroupName="button" />
<asp:RadioButton ID="rbtnNo2" runat="server" Text="×" GroupName="button" />
<%# Eval("Hj_YesOrNo")%>
</ItemTemplate> if (e.Row.RowState == DataControlRowState.Edit || ((int)(e.Row.RowState & DataControlRowState.Edit)) != 0)
{
RadioButton rdbl= e.Row.FindControl("rbtnYes") as RadioButton ;
//查询
}
if (e.Row.RowState == DataControlRowState.Edit)e.Row.RowState == (DataControlRowState.Alternate|DataControlRowState.Edit)
详细请看
http://blog.xunbin.com/Article/2010/9/2/187/
<%@ 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="6" 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" RepeatDirection="Horizontal">
<asp:ListItem Text="男"></asp:ListItem>
<asp:ListItem Text="女"></asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="NewGender" runat="server" RepeatDirection="Horizontal">
<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 HeaderText="照片">
<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>
{
TextBox tb = (TextBox)GvUpdateApply.Rows[row].Cells[0].FindControl("txtcardid");
。
}这样,不管你怎么取控件都取的出来
如果行的话我也不用来问了。这个只能取出Item里面的控件。Edit里面的取不出来的
呵呵。我已经说啦。不用事件的。用事件的话RowUpdating事件是可以拿到的。