关闭

Get GridView selected row DataKey in Javascript

606人阅读 评论(0) 收藏 举报
分类:
<asp:TemplateField ShowHeader="False">    <ItemTemplate>      <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("JobID") %>' />    </ItemTemplate></asp:TemplateField><asp:TemplateField Visible="False">    <ItemTemplate>      <asp:LinkButton ID="lnkSelect" runat="server" CommandName="select" Text="Select" />    </ItemTemplate></asp:TemplateField>

Then on the OnRowDataBind have code to set the selected row

protected virtual void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Click to highlight row
        Control lnkSelect = e.Row.FindControl("lnkSelect");
        if (lnkSelect != null)
        {
            StringBuilder click = new StringBuilder();
            click.AppendLine(m_View.Page.ClientScript.GetPostBackClientHyperlink(lnkSelect, String.Empty));
            click.AppendLine(String.Format("onGridViewRowSelected('{0}')", e.Row.RowIndex));
            e.Row.Attributes.Add("onclick", click.ToString());
        }
    }            
}

And then in the Javascript I have code like this

<script type="text/javascript">

var selectedRowIndex = null;

function onGridViewRowSelected(rowIndex)
{        
    selectedRowIndex = rowIndex;
}

function editItem()
{   
    if (selectedRowIndex == null) return;

    var gridView = document.getElementById('<%= GridView1.ClientID %>');                
    var cell = gridView.rows[parseInt(selectedRowIndex)+1].cells[0];        
    var hidID = cell.childNodes[0];        
    window.open('JobTypeEdit.aspx?id=' + hidID.value);
}

</script>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1105007次
    • 积分:10286
    • 等级:
    • 排名:第1632名
    • 原创:52篇
    • 转载:389篇
    • 译文:4篇
    • 评论:176条
    最新评论