之前,我有我想RowCommand執行RowCreate重裝或像這樣
<asp:GridView>
<asp:TemplateField HeaderText="PsyHealth">
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="PsyHealth" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="-">
<ItemTemplate>
<asp:LinkButton ID="Gen" CommandName="Gen" runat="server" Text="gen" />
</ItemTemplate>
</asp:TemplateField>
和
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dataItem = e.Row.DataItem as ViewModels.UserTestorViewModel;
var psyHealth = e.Row.FindControl("PsyHealth") as PlaceHolder;
if (psyHealth != null)
{
psyHealth.Controls.Add(dataItem.PsyHeath);
}
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//bla bla bla
}
,但是當我點擊頁面上的Gen
LinkButton
。 GridView1_RowCreated
首先被觸發並拋出錯誤Object reference not set to an instance of an object
,因爲e.Row.DataItem
爲空。
編輯:代碼
protected void Page_Load(object sender, EventArgs e)
{
List<ViewModels.UserTestorViewModel> utViewModelList = new List<ViewModels.UserTestorViewModel> { };
utViewModelList = utRepo.GetUserTestorViewModelListByHrId();
this.GridView1.DataSource = utViewModelList;
this.GridView1.DataBind();
if (!IsPostBack)
{
}
}
protected void Page_Init(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
}
你可以發佈你的RowCreated代碼?如果您將完整的表單和代碼放在後面,會更好。 – 2011-05-26 08:55:21
我更新了關於'GridView1_RowCreated'的帖子。 'GridView1_RowCommand'沒有代碼,問題是我永遠不會跑到那裏。 – hbrls 2011-05-26 09:05:11
你可以確定,你的gridview在rebback上回發嗎? – 2011-05-26 09:06:22