ASP.Net Gridview,如何根据 ID (DataKey) 激活编辑模式 [英] ASP.Net Gridview, How to activate Edit Mode based on ID (DataKey)

查看:11
本文介绍了ASP.Net Gridview,如何根据 ID (DataKey) 激活编辑模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,我们称之为 SourceTypes.aspx,它有一个显示源类型列表的 GridView.GridView 的一部分是 DataKey,SourceTypeID.如果通过查询字符串将源 TypeID 传递到页面,如何根据 SourceTypeID 将 Gridview 置于相应行的编辑模式?

I have a page, lets call it SourceTypes.aspx, that has a a GridView that is displaying a list of Source Types. Part of the GridView is a DataKey, SourceTypeID. If source TypeID is passed to the page via a query sting, how to I put the Gridview into Edit mode for the appropriate row based on the SourceTypeID?

GridView 绑定到 SQlDataSource 对象.

The GridView is bound to a SQlDataSource object.

我有一种感觉,当答案出现时我要踢自己!!

I have a feeling I am going to kick myself when the answer appears!!

我查看了以编程方式将 gridview 行置于编辑模式 但有些缺乏细节

I have looked at Putting a gridview row in edit mode programmatically but it is some what lacking in specifics

推荐答案

首先,非常感谢 Steve Robins 让我走上正轨.我的方法略有不同,部分原因是我没有使用 DataBind 方法.

First of all, many thanks to Steve Robins for getting me started down the right track. My approach was slightly different, partial due to the fact I was not using a DataBind method.

我使用了 DataView 的 OnRowDataBound 和 OnDataBound 事件的组合.OnRowDataBound 事件确定要进入编辑模式的行的索引.OnDataBound 事件设置索引并重新绑定 DataView.

I use a combination of the OnRowDataBound and OnDataBound events of the DataView. The OnRowDataBound event to determine the index of the row to be put into edit mode. The OnDataBound event sets the index and rebinds the DataView.

一个变量用来保证View不会连续反弹.

A variable is used to ensure that the View is not continuously rebound.

为了简洁起见,这里是 aspx 页面的症结所在.

Here is the crux of the aspx page edited down for brevity.

<asp:GridView ID="GridView1" runat="server" 
              DataSourceID="CreativeTypeDS"
              AutoGenerateColumns="False" 
              DataKeyNames="SourceCreativeTypeID" 
              EmptyDataText="No Cretive Types at this time."
              CellPadding="3" CellSpacing="1"
              OnRowDataBound="GridView1_RowDataBound"
              OnDataBound="GridView1_DataBound" >
        <Columns>
           [Template Columns Here]
        </Columns>
</asp:GridView>

<asp:SqlDataSource ID="CreativeTypeDS" runat="server" 
                   SelectCommand="cmsSourceCreativeTypeSel"
                   SelectCommandType="StoredProcedure" 
           UpdateCommand="cmsSourceCreativeTypeUpd"
                   UpdateCommandType="StoredProcedure">
    <SelectParameters>                
       <asp:Parameter Name="Active" DefaultValue="0" />
    </SelectParameters>
    <UpdateParameters>
       <asp:Parameter Name="SourceCreativeTypeID" Type="Int32" />
       <asp:Parameter Name="SourceCategoryID" Type="Int32"/>
       <asp:Parameter Name="Name" Type="String" />
       <asp:Parameter Name="Active" Type="Boolean" />
     </UpdateParameters>
</asp:SqlDataSource>

现在是背后的代码.

 public partial class SourceCreativeTypes : System.Web.UI.Page
 {
    private int? EditIndex = null;
    private bool GridRebound = false;
    protected override void OnPreInit(EventArgs e)        
    {
       CreativeTypeDS.ConnectionString = "[CONNECTION STRING MAGIC HERE]";
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {                
            //Dont Want to set edit row manualy if post back
            GridRebound = true;
        }
    }

    //Use the Row Databound Event to find the row to put into edit mode 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.QueryString["ID"]))
        {
            //Get Target ID from Query String
            string sSourceCreativeTypeID = Request.QueryString["ID"];
            //Get data for row being bound
            DataRowView rowView = (DataRowView)e.Row.DataItem;

            // Set index if we are in a "normal row", the row data 
            // has been retrieved
            // and the Supplied ID matches that of this row
            if ((e.Row.RowState == DataControlRowState.Normal || 
                e.Row.RowState == DataControlRowState.Alternate) 
                &&
                (rowView != null && 
                 rowView["SourceCreativeTypeID"].ToString() == sSourceCreativeTypeID)
                )
            {                   
                EditIndex = e.Row.RowIndex;                   
            }
        }
    }

    /* Use the Datbound Event to set the required row to index mode
     * Then Rebind the grid. Rebinding in Row Databound does not work
     * for Reasons unknown */
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        //Set Gridview edit index if one is supplied and page is not a post back
        if (!GridRebound && EditIndex != null)
        {
            //Setting GridRebound ensures this only happens once
            GridRebound = true;
            GridView1.EditIndex = (int)EditIndex;
            GridView1.DataBind();
        }
     }
   } 
}

希望在史蒂夫和我自己之间,我们可以帮助你们中的一些人

Hopefully between Steve and Myself we help a few of you out there

这篇关于ASP.Net Gridview,如何根据 ID (DataKey) 激活编辑模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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