【问题标题】:Calling RowCommand "Select" programmatically on a GridView在 GridView 上以编程方式调用 RowCommand“选择”
【发布时间】:2014-10-16 13:29:56
【问题描述】:

我的目标是根据请求参数在 gridview 中选择一行并打开一个 FormView

首先,我在 Page_Load 中获取我的参数:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string param_id = Request["id"];

        if (!string.IsNullOrEmpty(param_id))
        {
            SelectRowByDataKeyValue(param_id);
        }
    }
}

基于这个tutorial我想在我的GridView(称为gvPrincipal)中获取相应的行并选择它:

public void SelectRowByDataKeyValue(string value)
{
    gvPrincipal.DataBind();

    int tmpSelectedIndex = 0;
    int tmpPageIndex = 0;

    for (int page = 0; page < gvPrincipal.PageCount; page++)
    {
        gvPrincipal.PageIndex = page;

        gvPrincipal.DataBind();

        for (int key = 0; key < gvPrincipal.DataKeys.Count; key++)
        {
            if (!string.IsNullOrEmpty(value) && value.Equals(Convert.ToString(gvPrincipal.DataKeys[key].Value)))
            {
                tmpSelectedIndex = key;
                tmpPageIndex = page;
                break;
            }
        }
    }

    gvPrincipal.PageIndex = tmpPageIndex;
    gvPrincipal.SelectedIndex = tmpSelectedIndex;

    gvPrincipal.SelectRow(tmpSelectedIndex);

}

问题 1:在此之后,找到值,gvPrincipal.SelectedIndex 是正确的,但 gvPrincipal.SelectedValue 大部分时间给我一个不同的值

protected void gvPrincipal_SelectedIndexChanged(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine(String.Format("Selected Index : {0}, SelectedValue : {1}", gvPrincipal.SelectedIndex, /*wrong value*/gvPrincipal.SelectedValue));
}

问题 2:在 GridView 中,页面索引按预期更新,但行没有更新(应该是彩色的)

此外,每一行都有一个 ImageButton:

<Columns>
...
<asp:TemplateField>
      <ItemTemplate>
            <asp:ImageButton ID="ImgBtnDetail" runat="server" 
                CommandName="Select"
                CausesValidation="False" ImageUrl="~/Images/icon_detail_16.gif"/>
      </ItemTemplate>
</asp:TemplateField>
...
</Columns>

单击此按钮会引发 gvPrincipal_RowCommand(),它将 FormView 设置为只读模式并显示详细信息:

protected void gvPrincipal_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //displays infos
}

FormView 数据源使用 gvPrincipalValue 更新:

        <asp:ObjectDataSource ID="odsfvPrincipalDetail" runat="server" TypeName="crm.dal"
        SelectMethod="SelectElementById">        
        <SelectParameters>
            <asp:ControlParameter ControlID="gvPrincipal" Name="id" PropertyName="SelectedValue" Type="String" />
        </SelectParameters>          
    </asp:ObjectDataSource>

我想在 SelectRowByDataKeyValue 中以编程方式触发相同的行为; 这是我的尝试:

public void SelectRowByDataKeyValue(string value)
{
    ...

    GridViewRow row = gvPrincipal.SelectedRow;

    ImageButton ImgBtnDetail = (ImageButton)row.FindControl("ImgBtnDetail");

    CommandEventArgs args = new CommandEventArgs("Select", null);

    gvPrincipal_RowCommand(this, new GridViewCommandEventArgs(row, ImgBtnDetail, args));

}

不幸的是什么都没发生

没有保持未选中的行是我最关心的问题,我该如何克服这个问题?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    这是您可以在 C# 中手动调用 rowCommand 的方式

    MyGridView_RowCommand(MyGridView, new GridViewCommandEventArgs(imgButton, new CommandEventArgs(imgButton.CommandName, imgButton.CommandArgument)));

    MyGridView 是一个 GridView。 imgButton 是 ImageButton,尤其是位于 MyGridView 中的 GridViewRow,我正在调用 RowCommand。我在 CheckBox 上调用此 RowCommand,它也位于 MyGridView 中,但复选框通常不能正常处理为行命令中的图像按钮。

        protected void MyCheckBoxInGrid_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = sender as CheckBox;
    
            GridViewRow row = ((GridViewRow)chk.NamingContainer);
    
    
    
            if (chk.Checked)
            {
    
    
                ImageButton imgButton = row.FindControl("ImgDetail") as ImageButton;
                MyGridView_RowCommand(MyGridView, new GridViewCommandEventArgs(imgButton , new CommandEventArgs(imgButton .CommandName, imgButton .CommandArgument)));
            }}
    

    问候! 扎希德·阿什拉夫

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多