GridView更新事件错误 [英] GridView Update Event error
问题描述
嗨!
我的网站上有一个gridview:
< asp:GridView ID = GridView1 < span class =code-attribute> runat = server AlternatingRowStyle-CssClass = gridview_alter
AutoGenerateColumns = False EmptyDataText = GridLines = 无 OnRowCancelingEdit = RowCancelingEdit
< span class =code-attribute> OnRowDataBound = GridView1_RowDataBound OnRowDeleting = RowDeleting OnRowEditing = RowEditing
OnRowUpdating = < span class =code-keyword> RowUpdating ShowHeaderWhenEmpty = 真 宽度 = 820px >
< < span class =code-leadattribute> AlternatingRowStyle CssClass = gridview_alter / >
< 列 >
< asp:TemplateField HeaderText =< /跨度> Leírás 可见 = false / >
< asp:BoundField DataField = 描述 HeaderStyle-Width = 150 HeaderText = Leírás HtmlEnco de = false >
< HeaderStyle 宽度 = 150px < span class =code-attribute> / >
< / asp:BoundField >
< asp:BoundField < span class =code-attribute> HeaderText = Kritérium DataField = IndexNumber / >
< span class =code-keyword>< asp:BoundField DataField = 权重 HeaderStyle-Width = 50 HeaderText = Súly >
< Hea derStyle 宽度 = 50px / >
< / asp:BoundField < span class =code-keyword>>
< asp:TemplateField HeaderStyle-Width = < span class =code-keyword> 100 HeaderText = Céltípusa >
< 跨度> EditItemTemplate >
< asp:DropDownList ID = DropDownList1
< span class =code-attribute> < span class =code-attribute> runat = server AutoPostBack = False >
< / asp:DropDownList >
< / EditItemTemplate >
< ItemTemplate >
< asp:标签 ID = lblGoalType runat = 服务器 > < / asp :标签 >
< / ItemTemplate >
< HeaderStyle 宽度 = 100px / >
< / asp:TemplateField >
< asp:CommandField DeleteText = Törlés HeaderStyle-Width = 60 显示DeleteButton = true >
< HeaderStyle 宽度 = 60px < span class =code-attribute> / >
< / asp:CommandField >
< asp:CommandField < span class =code-attribute> CancelText = Mégse DeleteText = Törlés EditText = Szerkesztés
HeaderStyle-Width = 60 ShowEditButton = True UpdateText = Frissítés
ValidationGroup = Leírás,Súly >
< HeaderStyle 宽度 = 60px < span class =code-keyword> / >
< / asp:CommandField >
< /列 >
< / asp:GridView >
Céltípusa列从列表中获取数据,包含1,2或3.在后面的代码中:
protected void GridView1_RowDataBound( object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
目标行=(目标)e.Row.DataItem;
标签lblGoalType =(标签)e.Row.FindControl( lblGoalType);
e.Row.Cells [ 0 ]。文本= e.Row.Cells [ 0 ]。Text.Replace( \ n, < br />);
switch (row.GoalTypeId)
{
case 1 :
lblGoalType.Text = Személyes跨度>;
break ;
case 2 :
lblGoalType.Text = Egyéni;
break ;
case 3 :
lblGoalType.Text = Szervezeti;
break ;
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 引用Delete LinkButton
LinkButton db =(LinkButton)e.Row.Cells [ 5 ]。控制[ 0 ];
db.OnClientClick = return confirm('Biztos benne,hogytörölniakarjaakiválasztottcélt?'); 跨度>;
}
}
我给指定数字字符串。
我的问题是我需要用下拉列表更新单元格,但是当你按下gridview上的Edit按钮时我得到一个错误消息:对象引用未设置为对象的实例。
我被困在这里,不知道,请帮忙。
引用:但是当你按下gridview上的Edit按钮时我得到一个错误消息:对象引用未设置为对象的实例。
调试事件
OnRowEditing = RowEditing
。
参考最受欢迎的答案 Sergey (@SAKryukov)对象引用未设置为对象的实例,请参阅下面的代码。 [ ^ ]。
我认为你根本没有将数据绑定到控件上,所以尝试绑定它们
Hi!
I've have a gridview on my site:
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-CssClass="gridview_alter"
AutoGenerateColumns="False" EmptyDataText=" " GridLines="None" OnRowCancelingEdit="RowCancelingEdit"
OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="RowDeleting" OnRowEditing="RowEditing"
OnRowUpdating="RowUpdating" ShowHeaderWhenEmpty="True" Width="820px">
<AlternatingRowStyle CssClass="gridview_alter" />
<Columns>
<asp:TemplateField HeaderText="Leírás" Visible="false" />
<asp:BoundField DataField="Description" HeaderStyle-Width="150" HeaderText="Leírás" HtmlEncode="false">
<HeaderStyle Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="Kritérium" DataField="IndexNumber" />
<asp:BoundField DataField="Weight" HeaderStyle-Width="50" HeaderText="Súly">
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:TemplateField HeaderStyle-Width="100" HeaderText="Cél típusa">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server" AutoPostBack="False">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblGoalType" runat="server"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:CommandField DeleteText="Törlés" HeaderStyle-Width="60" ShowDeleteButton="true">
<HeaderStyle Width="60px" />
</asp:CommandField>
<asp:CommandField CancelText="Mégse" DeleteText="Törlés" EditText="Szerkesztés"
HeaderStyle-Width="60" ShowEditButton="True" UpdateText="Frissítés"
ValidationGroup="Leírás, Súly">
<HeaderStyle Width="60px" />
</asp:CommandField>
</Columns>
</asp:GridView>
The "Cél típusa" column gets data from a list, that contains 1, 2 or 3. In code behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Goal row = (Goal)e.Row.DataItem;
Label lblGoalType = (Label)e.Row.FindControl("lblGoalType");
e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("\n", "<br />");
switch (row.GoalTypeId)
{
case 1:
lblGoalType.Text = "Személyes";
break;
case 2:
lblGoalType.Text = "Egyéni";
break;
case 3:
lblGoalType.Text = "Szervezeti";
break;
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
// reference the Delete LinkButton
LinkButton db = (LinkButton)e.Row.Cells[5].Controls[0];
db.OnClientClick = "return confirm('Biztos benne, hogy törölni akarja a kiválasztott célt?');";
}
}
I give strings to the specified numbers.
My problem is that I need to update the cells with a dropdownlist, but when u press the Edit button on the gridview i get an error msg: Object reference not set to an instance of an object.
I am stuck here, got no idea, please help.
Quote:but when u press the Edit button on the gridview i get an error msg: Object reference not set to an instance of an object.
Debug the Event
OnRowEditing="RowEditing"
.
Refer the most popular answer by Sergey ( @SAKryukov ) "Object Reference not set to Instance of an object", please see the code below.[^].
i think you are not at all binding the data to controls so try by binding them
这篇关于GridView更新事件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!