【问题标题】:How to display selected row value/text inside the dropdown如何在下拉列表中显示选定的行值/文本
【发布时间】:2020-02-06 12:09:05
【问题描述】:

所以我是使用 ASP.net 的新手,我很难在下拉列表中显示所选行的值/文本。我只想在 griview 中选择一行,当我选择时,其中的值应该填充文本框和下拉列表,有人可以帮助我吗?谢谢

这是我的代码,正如您在 LoadData() 部分中看到的那样,我使用了 .findtext() 但它输出错误:对象引用未设置为对象的实例。

protected void grdRecentCases_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
           e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
           e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdRecentCases, "Select$" + e.Row.RowIndex);
       }
   }


   protected void grdRecentCases_RowCommand(Object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "Select")
       {
           LoadData(Convert.ToInt32(e.CommandArgument));

       }
   }

   ///<summary> LoadData is used to populate inputboxes with the value of the selected griview row</summary>
   ///<param name="rowNumber"> Indicates whether there is a selected row or non</param>
   private void LoadData(int? rowNumber = null)
   {
       //if rowNumber is null use GridView1.SelectedIndex
       var index = rowNumber ?? grdRecentCases.SelectedIndex;

       //Populate the input box with the value of selected row.
       GridViewRow gr = grdRecentCases.Rows[index];
       txtDepartmentCase.Text = gr.Cells[2].Text;
       txtLabCase.Text = gr.Cells[5].Text;
       txtIncidentReportDate.Text = gr.Cells[6].Text;
       txtCaseKey.Text = gr.Cells[1].Text;
       drpDepartment.Items.FindByText(gr.Cells[3].Text).Selected = true;
       drpCharge.Items.FindByText(gr.Cells[4].Text).Selected = true;

   }

HTML代码

<table class="style2">
            <tr>
                <td class="style3">
                    Department Case #
                </td>
                <td>
                    <asp:TextBox ID="txtDepartmentCase" runat="server" Enabled="False"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Department
                </td>
                <td>
                    <asp:DropDownList ID="drpDepartment" runat="server" Height="18px" Width="153px" Enabled="False"
                        AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="drpDepartment_SelectedIndexChanged"
                        Visible="true">
                    </asp:DropDownList>
                    <asp:TextBox ID="txtDepartment" runat="server" Enabled="False"></asp:TextBox>


                </td>
            </tr>
            <tr>
                <td class="style3">
                    Charge
                </td>
                <td>
                    <asp:DropDownList ID="drpCharge" runat="server" Height="22px" Width="153px" Enabled="False"
                        AppendDataBoundItems="true" AutoPostBack="true"  OnSelectedIndexChanged="drpCharge_SelectedIndexChanged"
                        Visible="true">
                    </asp:DropDownList>
                    <asp:TextBox ID="txtCharge" runat="server" Enabled="False"></asp:TextBox>


                </td>
            </tr>
            <tr>
                <td class="style3">
                    Lab Case #
                </td>
                <td>
                    <asp:TextBox ID="txtLabCase" runat="server" Enabled="False"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    Incident Report Date
                </td>
                <td>
                    <asp:TextBox ID="txtIncidentReportDate" runat="server" Enabled="False"></asp:TextBox>
                </td>
            </tr>
        </table>

【问题讨论】:

  • 您是否在 gr.Cells[3].Text 中获得任何价值?
  • 只要单击gridview 中的任何行,就会执行错误。应用程序不会运行
  • 在哪一行显示错误?在 grdRecentCases_RowCommand 的开头放一个断点
  • drpDepartment.Items.FindByText(gr.Cells[3].Text).Selected = true;这里
  • 然后可以查看gr.Cells[3].Text中的文字是什么。调试代码并在到达该行之前将鼠标悬停在 gr.Cells[3].Text 上。或选择它添加做快速手表或添加手表

标签: c# html asp.net gridview dropdown


【解决方案1】:

正如您所提到的,要在 griview 中选择一行,当我选择时,其中的值应该填充文本框和下拉列表。

希望这可以帮助你。 创建datagridview事件,RowHeaderMouseClick

     private void datagridview_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        string selectedDoc = datagridview.Rows[datagridview.SelectedRows[0].Index].Cells["column name"].Value.ToString() ;

        combobox.Text = selectedDoc;
    }

【讨论】:

    猜你喜欢
    • 2014-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2020-07-10
    • 2021-09-27
    • 2020-03-06
    相关资源
    最近更新 更多