Gridview RowCommand不起作用 [英] Gridview RowCommand not working

查看:124
本文介绍了Gridview RowCommand不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试获取在GridView中触发的rowcommand事件时遇到问题。我遵循MSDNet的代码示例,但我无法弄清楚它为什么不起作用。代码如下。

pre $ < asp:GridView ID =GridViewProductsrunat =serverAutoGenerateColumns =False
BackColor =WhiteBorderColor =#999999BorderStyle =SolidBorderWidth =1px
CellPadding =5CellSpacing =1DataKeyNames =Pkey
DataSourceID =SqlDataSourceProducts ForeColor =BlackGridLines =垂直>
< FooterStyle BackColor =#CCCCCC/>
<列>
< asp:BoundField DataField =ProductHeaderText =Product>
< HeaderStyle Horizo​​ntalAlign =Left/>
< ItemStyle Horizo​​ntalAlign =Left/>
< / asp:BoundField>
< asp:TemplateField HeaderText =兴趣>
< ItemTemplate>
< asp:DropDownList ID =ddlProductInterestrunat =serverSelectedValue ='<%#Bind(ProductInterest)%>'>
< asp:ListItem>< / asp:ListItem>
< asp:ListItem>低< / asp:ListItem>
< asp:ListItem>中< / asp:ListItem>
< asp:ListItem>高< / asp:ListItem>
< asp:ListItem>无< / asp:ListItem>
< / asp:DropDownList>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =>
< ItemTemplate>
< asp:Button runat =serverID =TestButtonText =ButtonCommandName =Test
CommandArgument =<%#CType(Container,GridViewRow).RowIndex%> ; />
< / ItemTemplate>
< HeaderStyle Horizo​​ntalAlign =center/>
< ItemStyle Horizo​​ntalAlign =center/>
< / asp:TemplateField>
< /列>
< SelectedRowStyle BackColor =#000099Font-Bold =TrueForeColor =White/>
< HeaderStyle BackColor =SilverFont-Bold =TrueForeColor =Black/>
< AlternatingRowStyle BackColor =#CCCCCC/>
< / asp:GridView>

++后面的代码+++

Sub GridViewProducts_RowCommand(ByVal sender As Object,ByVal e As GridViewCommandEventArgs)

如果e.CommandName =测试然后

Dim index = Convert.ToInt32(e.CommandArgument)
Dim row = GridViewProducts.Rows(index)
Dim MyString As String = row.Cells(0) .Text

strSQL =INSERT INTO tblClosedProducts(& _
Product,ClosedBy,DateClosed& _
)VALUES(& _
@Product,@ClosedBy,@DateClosed& _


Dim MyParameters1 As SqlParameter()= {_
New SqlParameter(@ Product, SqlDbType.VarChar),_
新的SqlParameter(@ ClosedBy,SqlDbType.VarChar),_
新的SqlParameter(@ DateClosed,SqlDbType.SmallDateTime)_
}

MyParameters1(0).Value = row.Cells(0).Text
MyParameters1(1).Value = GetInfo.GetFullName(UCase(Right(HttpContext.Current.User.Identity.Name .ToString(),4)))
MyParameters1(2).Value = DateAdd(h,-1,Now())

objData.SQLExecuteNonQuery(strSQL,CommandType.Text ,MyParameters1)

End If

End Sub




尝试添加 onrowcommand =>解决方案

GridViewProducts_RowCommand看起来像这样:

 < asp:GridView ID =GridViewProducts runat =serverAutoGenerateColumns =False
BackColor =WhiteBorderColor =#999999BorderStyle =SolidBorderWidth =1px
CellPadding =5CellSpacing =1DataKeyNames =Pkey
DataSourceID =SqlDataSourceProductsForeColor =BlackGridLines =垂直
onrowcommand =GridViewProducts_RowCommand>


I am having problems trying to get a rowcommand event to fire in a gridview. I followed the code example from MSDNet but I cannot figure out why it is not working. The code is below. Thank you.

<asp:GridView ID="GridViewProducts" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 
    CellPadding="5" CellSpacing="1" DataKeyNames="Pkey" 
    DataSourceID="SqlDataSourceProducts" ForeColor="Black" GridLines="Vertical">
    <FooterStyle BackColor="#CCCCCC" />
    <PagerSettings PageButtonCount="20" />
    <Columns>
        <asp:BoundField DataField="Product" HeaderText="Product" >
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="Interest">
            <ItemTemplate>
                <asp:DropDownList ID="ddlProductInterest" runat="server" SelectedValue='<%# Bind("ProductInterest") %>'>
                    <asp:ListItem></asp:ListItem>
                    <asp:ListItem>Low</asp:ListItem>
                    <asp:ListItem>Medium</asp:ListItem>
                    <asp:ListItem>High</asp:ListItem>
                    <asp:ListItem>None</asp:ListItem>
                </asp:DropDownList>
             </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:Button runat="server" ID="TestButton" Text="Button" CommandName="Test" 
                CommandArgument="<%# CType(Container, GridViewRow).RowIndex %>" />
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="center" />
            <ItemStyle HorizontalAlign="center" />
       </asp:TemplateField>
    </Columns>
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="Black" />
    <AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>

++Code Behind +++

Sub GridViewProducts_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)

   If e.CommandName = "Test" Then

    Dim index = Convert.ToInt32(e.CommandArgument)
    Dim row = GridViewProducts.Rows(index)
    Dim MyString As String = row.Cells(0).Text

    strSQL = "INSERT INTO tblClosedProducts (" & _
         "Product, ClosedBy, DateClosed " & _
         ") VALUES (" & _
         "@Product, @ClosedBy, @DateClosed " & _
         ")"

    Dim MyParameters1 As SqlParameter() = { _
       New SqlParameter("@Product", SqlDbType.VarChar), _
       New SqlParameter("@ClosedBy", SqlDbType.VarChar), _
       New SqlParameter("@DateClosed", SqlDbType.SmallDateTime) _
    }

    MyParameters1(0).Value = row.Cells(0).Text
    MyParameters1(1).Value = GetInfo.GetFullName(UCase(Right(HttpContext.Current.User.Identity.Name.ToString(), 4)))
    MyParameters1(2).Value = DateAdd("h", -1, Now())

    objData.SQLExecuteNonQuery(strSQL, CommandType.Text, MyParameters1)

  End If

End Sub

解决方案

Your gridview doesnt have the event wired up in its markup.

Try adding in onrowcommand="GridViewProducts_RowCommand" so it looks like this:

<asp:GridView ID="GridViewProducts" runat="server" AutoGenerateColumns="False" 
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 
CellPadding="5" CellSpacing="1" DataKeyNames="Pkey" 
DataSourceID="SqlDataSourceProducts" ForeColor="Black" GridLines="Vertical"
onrowcommand="GridViewProducts_RowCommand">

这篇关于Gridview RowCommand不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆