使用模板列时如何取到当前的行值?
我使用模板列,里面放置了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
}
}