【问题标题】:How do I run the RowDataBound method in the onClick method below?如何在下面的 onClick 方法中运行 RowDataBound 方法?
【发布时间】:2018-11-11 09:50:31
【问题描述】:

我有一个 GridView 如下

`<asp:GridView ID="gvSearchAll" runat="server" AutoGenerateColumns="False"
     OnPageIndexChanging="searchAll_PageIndexChanging" 
     onrowdatabound="OnRowDataBound">
         <Columns>
              <asp:BoundField DataField="A" HeaderText="A"/>
              <asp:BoundField DataField="B" HeaderText="B"/>
              <asp:BoundField DataField="C" HeaderText="C" />
              <asp:TemplateField HeaderText="Select">
                  <ItemTemplate>
                     <asp:CheckBox ID="RowSelector" runat="server" onclick="checkRadioBtn(this);" />
                  </ItemTemplate>
              </asp:TemplateField>                  
         </Columns>          
 </asp:GridView>`

CodeBehind 上的 OnRowDataBound,我有以下内容:

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {               
           e.Row.Cells[3].Attributes.Add("onclick", string.Format("DisplayDetails('{0}');", e.Row.RowIndex + 1));
           e.Row.Attributes["onmouseover"] = "onMouseOver('" + (e.Row.RowIndex + 1) + "')";
           e.Row.Attributes["onmouseout"] = "onMouseOut('" + (e.Row.RowIndex + 1) + "')";
        }        

    }

现在我真正想要的是在单击复选框时运行 DisplayDetails 函数。

 function DisplayDetails(row) {
    var gridView = document.getElementById('gvSearchAll');  
    document.getElementById("A").value = gridView.rows[row].cells[1].innerText;
    document.getElementById("B").value = gridView.rows[row].cells[0].innerText;      
}

我想做的是,当单击复选框时,我想将特定复选框的 A 和 B 列数据填充到某个文本字段。

onClick 函数 checkRadioBtn(this) 执行其他操作。

现在,当前,每当我单击 CheckBox 的整个单元格时,我都会执行显示详细信息功能。 我需要的是在 checkRadioBtn(this) 函数中执行 DisplayDetails 函数,但为此我需要类似 (this.row_index) 之类的东西,但我不知道该怎么做。

请帮忙。

【问题讨论】:

    标签: javascript c# asp.net gridview


    【解决方案1】:

    为什么要使用复选框,您可以通过使用链接按钮轻松实现它

    喜欢这个

    <asp:GridView ID="gvSearchAll" runat="server" AutoGenerateColumns="False"
         OnPageIndexChanging="searchAll_PageIndexChanging"  >
             <Columns>
                  <asp:BoundField DataField="A" HeaderText="A"/>
                  <asp:BoundField DataField="B" HeaderText="B"/>
                  <asp:BoundField DataField="C" HeaderText="C" />
                  <asp:TemplateField HeaderText="Select">
                     <ItemTemplate  >                
                              <asp:LinkButton  ID="RowSelector"  Text="Details"   OnClientClick = "return Details(this)"  runat="server"  CommandName="Select" ></asp:LinkButton>
                   </ItemTemplate>                 
             </Columns>          
     </asp:GridView>
    

    脚本

     function Details(lnk) {
                 var row = lnk.parentNode.parentNode;
                 var rowIndex = row.rowIndex - 1;
                 document.getElementById("A").value = gridView.rows[row].cells[1].innerText;
                 document.getElementById("B").value = gridView.rows[row].cells[0].innerText; 
             }
    

    【讨论】:

    • 嗨阿伦。感谢您的回复。要求是一个复选框,这就是原因。行 = lnk.parentNode.parentNode;声明也适用于复选框吗?我现在就试一试。
    • 是的,告诉我如果它不起作用,我会尝试不同的方法
    • 嘿阿伦。完美运行。只需要更改 rowIndex = row.rowIndex;因为 (-1) 给出了前一行的值。非常感谢。
    • Laba row.rowIndex - 1;将显示当前行,因为行索引以 0 开始
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多