在GridView 中放置一个模板列,其中放一个LinkButton,请问,如何取得点击LinkButton时,所在的当前行GridViewRow 呢?2005没有e.Item了阿,请帮忙了... ....
解决方案 »
- 脚本调用webservic顺序出问题!
- 如何嵌套动态添加panel
- 目前使用FCKeditor控件,但是这个控件中好像不支持在页面中加入视频的操作??
- 建dataset或datatable
- M| 200分求 Windows的安装制作问题,如何写注册表 补分
- 请孟子大哥等高手救命!!!!
- atlas难题 思归等高手请进
- 朋友帮忙,在点击DataGrid得一个单元格得时候,怎么获得它得列号,即:Item.Cells[i] i 得值????
- 帮助:怎么得到客户窗口退出时间
- 错误提示:对于多个基表不支持动态 SQL 生成???
- Kaersoft_Forum不能运行,为何?
- 想问一下,csdn上的那种滚动带连接的广告怎么写?
{
// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = AuthorsGridView.Rows[index];
// Create a new ListItem object for the author in the row.
ListItem item = new ListItem();
item.Text = row.Cells[2].Text + " " + row.Cells[1].Text;
// If the author is not already in the ListBox, add the ListItem
// object to the Items collection of the ListBox control.
if(!AuthorsListBox.Items.Contains(item))
{
AuthorsListBox.Items.Add(item);
}
}
真的很难么?唉 急死我了... ...
<asp:GridView ID="GridView1" Runat="server" BorderWidth="1px" BackColor="White" GridLines="Vertical" CellPadding="4" BorderStyle="None" BorderColor="#DEDFDE" ForeColor="Black" AutoGenerateColumns="False" AllowPaging="True" PageSize="5">
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#6B696B"></HeaderStyle>
<AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
<Columns>
<asp:BoundField HeaderText="FirstName" DataField="FirstName"></asp:BoundField>
<asp:BoundField HeaderText="LastName" DataField="LastName"></asp:BoundField>
<asp:TemplateField HeaderText="Test Template">
<ItemTemplate>
<asp:Label ID="Id" text="Test Template" Runat=server ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#CE5D5A"></SelectedRowStyle>
<RowStyle BackColor="#F7F7DE"></RowStyle>
</asp:GridView>----------------------- Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
MyOninit()
End If
End Sub Private Sub MyOninit()
Dim strConn As String = "server=(local);database=Northwind;uid=sa;pwd="
Dim Conn As SqlConnection = New SqlConnection(strConn)
Conn.Open()
Dim sql As String = "select * from Employees"
Dim cmd As SqlCommand = New SqlCommand(sql, Conn)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet
da.Fill(ds, "myxTest")
GridView1.DataSource = ds.Tables(0).DefaultView
GridView1.DataBind()
Conn.Close() End Sub Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim row As GridViewRow = e.Row
Dim LbId As Label = CType(row.Cells(2).FindControl("Id"), Label) If Not LbId Is Nothing Then
Response.Write(LbId.Text & "<br>")
End If
End If End Sub但我不知道在下面这怎么分页,用原来的CurrentPageIndex 出错。555
Private Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged
' 'GridView1.CurrentPageIndex = e.NewPageIndex End Sub
模板列中LinkButton 的CommandArgument 属性的参数为行号,使用时,在RowCommand事件或其他这个GrideView 的事件中,这个按钮的CommandArgument 属性就是当前行的索引号了。
我也是结合MSDN才知道必须,提前给模板列中命令按钮的CommandArgument 赋值之后才可以使用的。
Code样例:
RowDataBound事件中:
{
LinkButton lb = ((LinkButton)e.Row.FindControl("LinkButtonSR"));
if (lb == null)
{
return;
}
lb.Enabled = true;
switch (Convert.ToInt32((e.Row.Cells[10].Text.Trim())))
{
case 1:
lb.CommandArgument = e.Row.DataItemIndex.ToString();
break;
... ...
}
}
RowCommand事件中的代码,则分页不会出错了。