GridView选择行 删除行问题
对GridView数据操作,我实现的功能是单击行,把这行的数据显示到几个文本框,同时行的底色也变掉;单击行最后的ImageButton按钮,删除这行。
现在问题是,我选择行,显示数据和颜色功能没什么问题,但是每次都要点
击2次ImageButton才能删除行,第一次点击它执行的还是SelectedIndexChanging的代码。
代码如下:
(发现去掉QuestionListRowDataBound中的代码就能点一次删除行了,但是这样的话当我单击行显示数据和行颜色功能就不起作用了)
protected void c_Question_GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
DisplayRowData(GetCurrentRowData(e.NewSelectedIndex), parameters);
}
protected void c_Question_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(
e.Row.Cells[0].Controls[1], string.Empty));
//e.Row.Cells[0].Controls[1]是LinkButton:id='c_LblQuestion'
}
}
protected void c_Question_GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRowDeleting(parameters,e.RowIndex);
}
<asp:GridView ID="c_Question_GridView" runat="server"
AlternatingRowStyle-BackColor="#f0efea" AutoGenerateColumns="False"
class="commonfont" HeaderStyle-BackColor="#edefe4"
onrowdatabound="c_Question_GridView_RowDataBound"
OnRowDeleting="c_Question_GridView_RowDeleting"
onselectedindexchanging="c_Question_GridView_SelectedIndexChanging"
SelectedRowStyle-BackColor="#63CCFB" ShowFooter="True" Width="100%">
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Question"
ItemStyle-HorizontalAlign="Left" ItemStyle-Width="92%">
<ItemTemplate>
<asp:LinkButton ID="c_LblQuestion" runat="server" CommandName="Select"
Text='<%# Eval("Ques") %>'></asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="92%" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Delete"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:ImageButton ID="c_ItemTemplate_Delete0" runat="server"
CommandName="Delete" ImageUrl="~/Images/icon_delete.gif" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#EDEFE4" />
<AlternatingRowStyle BackColor="#F0EFEA" />
</asp:GridView>
[解决办法]
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(
e.Row.Cells[0].Controls[1], string.Empty));
}
[解决办法]
单击行,把这行的数据显示到几个文本框,同时行的底色也变掉的功能可以用JS实现,这样不必调用服务器的选择事件了
protected void c_Question_GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)) { e.Row.Attributes.Add("onclick", "selectRow()"); } }