ASP.Net的GridView,如何基于ID激活编辑模式(DataKey) [英] ASP.Net Gridview, How to activate Edit Mode based on ID (DataKey)
问题描述
我有一个页面,让我们把它叫做SourceTypes.aspx,有一个被显示的源类型列表的GridView。在GridView的一部分是DataKey,SourceTypeID。如果源TYPEID通过查询刺痛,如何我把GridView控件进入编辑模式的基础上,SourceTypeID?
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!!
我已经看过<一个href=\"http://stackoverflow.com/questions/206983/putting-a-gridview-row-in-edit-mode-programmatically\">Putting在编辑模式下的GridView行编程的却是有些缺乏具体是什么
I have looked at Putting a gridview row in edit mode programmatically but it is some what lacking in specifics
推荐答案
首先,非常感谢史蒂夫·罗宾斯的让我开始走上了正轨。我的方法略有不同,部分是由于我不使用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.
我使用OnRowDataBound的组合和OnDataBound的数据视图的事件。的OnRowDataBound事件确定的行的索引被放入编辑模式。该OnDataBound事件设置索引并重新绑定数据视图。
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.
一个变量用于确保该视图不会持续反弹。
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>
和现在的code的后面。
And now for the code behind.
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屋!