使用模板列时如何取到当前的行值?

baddot 2004-05-12 09:09:04
我使用模板列,里面放置了RadioButtonList控件,我想点击RadioButtonList某项时,自动显示出这行的某些信息。

我使用了下面方法,大家看一下有没有更好的解决办法。

DataGrid1.aspx代码如下。
===========================================
<%@ Page language="c#" Codebehind="DataGrid1.aspx.cs" AutoEventWireup="false" Inherits="Example.DataGrid1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DataGrid1</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<h3>在DataGrid控件中添加一列RadioButton,当选择其中一个时,触发一个事件。</h3>
<P>
<asp:DataGrid id="myDataGrid" runat="server" AutoGenerateColumns="False" Font-Size="9pt">
<Columns>
<asp:TemplateColumn HeaderText="模板列">
<HeaderTemplate>
<FONT face="宋体">选择处理方式</FONT>
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButtonList id="myRadioButtonList" runat="server" Font-Size="9pt" AutoPostBack="True" OnSelectedIndexChanged="myRadioButtonList_SelectedIndexChanged">
<asp:ListItem Value="删除">删除</asp:ListItem>
<asp:ListItem Value="编辑">编辑</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="OrderID" HeaderText="OrderID"></asp:BoundColumn>
<asp:BoundColumn DataField="OrderDate" HeaderText="OrderDate"></asp:BoundColumn>
<asp:BoundColumn DataField="Freight" HeaderText="Freight"></asp:BoundColumn>
<asp:BoundColumn DataField="ShipName" HeaderText="ShipName"></asp:BoundColumn>
</Columns>
</asp:DataGrid></P>
<P>
<asp:Label id="myLabel" runat="server">Label</asp:Label></P>
</form>
</body>
</HTML>

======================================================================

下面是DataGrid1.aspx.cs代码。
======================================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace Example
{
public class DataGrid1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label myLabel;
protected System.Web.UI.WebControls.DataGrid myDataGrid;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}

public void myRadioButtonList_SelectedIndexChanged(object sender, System.EventArgs e)
{
int itemIndex = ((DataGridItem)((RadioButtonList)sender).Parent.Parent).ItemIndex;

myLabel.Text = ((RadioButtonList)sender).SelectedValue;
myLabel.Text += myDataGrid.DataKeys[itemIndex] + "操作";

BindData();
}

public void BindData()
{
string ConnStr = "Server=(local);User id=sa;Pwd=;Database=Northwind";

string query = "select top 5 OrderID,OrderDate,Freight,ShipName from ORDERS";

SqlCommand myCommand = new SqlCommand(query, new SqlConnection(ConnStr));
myCommand.Connection.Open();
SqlDataReader dr = myCommand.ExecuteReader();

myDataGrid.DataSource = dr;
myDataGrid.DataKeyField = "OrderID";
myDataGrid.DataBind();

dr.Close();
myCommand.Connection.Close();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

}
}

...全文
112 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuike519 2004-05-12
  • 打赏
  • 举报
回复
你的方法是对的!好像没有更好的方法!除非你使用的是带CommandName的控件比如(Button等),这样可以激发ItemCommand通过CommandName来判断同时可以通过e.Item得到当前行!

楼上的方法和搂主的是一样的只是写法不同而已!
kerling78 2004-05-12
  • 打赏
  • 举报
回复
我是这么做的,你参考以下(vb)
html页
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 64px" runat="server"
Font-Size="X-Small" Width="392px" AllowPaging="True" BorderColor="ActiveBorder" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4" Height="320px">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle ForeColor="#003300" BackColor="#FFFFFF"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="选择">
<ItemTemplate>
<asp:RadioButton id="RB" runat="server" AutoPostBack="True" OnCheckedChanged="rdo_CheckedChanged"></asp:RadioButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Mode="NumericPages"></PagerStyle>
</asp:datagrid>



在后台代码页加入这个函数。
Protected Sub rdo_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
'radiobutton 检验事件
Dim rdo As RadioButton = CType(sender, RadioButton)
Dim cell As TableCell = CType(rdo.Parent, TableCell)
Dim item As DataGridItem = CType(cell.Parent, DataGridItem)
Dim i As Integer
For i = 0 To DataGrid1.Items.Count - 1
If i <> item.ItemIndex Then
Dim r As RadioButton = CType(DataGrid1.Items(i).FindControl("rb"), RadioButton)
r.Checked = False
End If
Next


'取表中选定员素值
Dim j, count As Integer
Dim temp As String
Dim flag As Boolean
flag = False
count = DataGrid1.Items.Count
i = 0
j = 0
Do
Do While (i < count) And (flag = False)
i = i + 1
Dim RB As RadioButton = CType(DataGrid1.Items(i - 1).FindControl("RB"), RadioButton)
If RB.Checked = True Then
flag = True
number.Value = DataGrid1.Items(i - 1).Cells(1).Text
name.Value = DataGrid1.Items(i - 1).Cells(2).Text
End If
Loop
Loop Until (i = count) Or (flag = True)

End Sub
reaperwu 2004-05-12
  • 打赏
  • 举报
回复
((TextBox)e.Item.Cells[0].FindControl("tbTest")).Text

62,074

社区成员

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

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

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

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