找一个ASP.Net的GridView使用jQuery的则rowIndex [英] Get the rowIndex of an ASP.Net Gridview using jQuery
问题描述
喂,是有可能得到使用jQuery的GridView目前的rowIndex?
背景位:
我在模板字段使用服务器端的链接按钮,像这样从一个gridview删除行:
I delete rows from a gridview using a server side link button in a template field like so:
<asp:LinkButton CausesValidation="false" CommandName="Delete" ID="lnkDelete"
OnClientClick="javascript: return(confirm('Delete item?'));" runat="server" Text="Delete" />
哪些提示用户确认或取消删除。如果用户单击确定,然后调用上的codebehind此方法:
Which prompts the user to confirm or cancel the deletion. If the user clicks OK, it then calls this method on the codebehind:
protected void GridViewRowDeleting(object sender, GridViewDeleteEventArgs e)
{
this.gridview_uploads.EditIndex = -1;
if (!this.UploadsList.Count.Equals(0))
{
DocumentUpload upload = this.UploadsList[e.RowIndex];
if (upload != null)
{
this.UploadsList.RemoveAt(e.RowIndex);
this.BindInputGridview();
}
}
}
但是JavaScript的confirm(删除项目?)看起来有点蹩脚。
我更preFER使用像jQuery的对话,但如果我这样做,我不知道如何抓住使用这种方法则rowIndex(我可以弄清楚如何调用服务器code) 。
I'd much prefer to use something like JQuery's dialog, but if I do, I have no idea how to grab the rowindex using this approach (I can figure out how to call the server code).
任何想法?
很抱歉,如果这已经问 - 我做了这么一个拖网和Google搜索,但没有找到什么有用的东西。
Sorry if this has already been asked - I did a trawl of SO and Googled it but couldn't find anything useful.
推荐答案
我想通了如何使用__doPostBack方法(在JavaScript)来做到这一点。
I figured out how to do this using the __doPostBack method (in Javascript)
隐藏字段:
<asp:HiddenField ID="hidden_gridRowIndex" runat="server" />
在script标签:
$(document).ready
(
function () {
$("#div_dialog_confirmUploadDelete").dialog({
autoOpen: false
, title: "Delete upload"
, buttons: {
"OK": function () {
__doPostBack('GridViewRowDelete', $("#<%# hidden_gridRowIndex.ClientID %>").val());
$(this).dialog('close');
}
, "Cancel": function () { $(this).dialog('close'); }
}
});
});
function deleteConfirm(index) {
$("#<%# hidden_gridRowIndex.ClientID %>").val(index)
$("#div_dialog_confirmUploadDelete").dialog('open');
}
在GridView的:
<asp:TemplateField>
<ItemTemplate>
<a href="javascript: void(0);" onclick='javascript:return deleteConfirm(<%# Container.DataItemIndex %>);'>Delete</a>
</ItemTemplate>
</asp:TemplateField>
>>>在codebehind
在Page_Load中:
if (Request["__EVENTTARGET"] != null)
{
switch (Request["__EVENTTARGET"])
{
case "GridViewRowDelete":
if (Request["__EVENTARGUMENT"] != null)
{
int index = -1;
if (int.TryParse(Request["__EVENTARGUMENT"], out index))
{
this.GridViewRowDelete(index);
}
}
break;
}
}
在Page_Load中名为新的方法:
protected void GridViewRowDelete(int rowIndex)
{
this.gridview_uploads.EditIndex = -1;
if (!this.UploadsList.Count.Equals(0))
{
DocumentUpload upload = this.UploadsList[rowIndex];
if (upload != null)
{
this.UploadsList.RemoveAt(rowIndex);
this.BindInputGridview();
}
}
}
关于它的思考,我可能已经做出了ASP:HiddenField一个普通HTML隐藏输入控件作为服务器端永远不需要看到它
Thinking about it, I could have probably made the asp:HiddenField a regular html hidden input control as the server side never needs to see it.
感觉有点举步维艰可以随意朝我扔石头/提出改进意见。
It feels a bit ropey so feel free to throw stones at me / suggest improvements.
这篇关于找一个ASP.Net的GridView使用jQuery的则rowIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!