簡體   English   中英

如何從下拉列表中檢索文本?

[英]How to retrieve text from dropdownlist?

我必須將更改審核到數據庫中。 在大多數頁面上,這很完美,因為只有文本框-但是在我現在正在處理的頁面上,具有下拉列表的代碼無法正常工作。

<td>
  <asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDatacountry" DataTextField="country_name" DataValueField="country_id">
  </asp:DropDownList>

  <asp:SqlDataSource ID="SqlDatacountry" runat="server"  ConnectionString="<%$ ConnectionStrings:songtypecons %>" SelectCommand="SELECT * FROM [country_detail]"></asp:SqlDataSource>
</td>

后面的代碼:

string sql1 = "selectcust_fname,cust_mname,cust_lname,cust_birthdate,cust_gender,cust_address,cust_contact_num,cust_country,cust_state,cust_city,cust_zip from cust_detail where cust_id ='" + ds.Tables["filldata"].Rows[0].ItemArray[0].ToString() + "' ";
            SqlDataAdapter adpt1 = new SqlDataAdapter(sql1, con);
            DataSet ds1 = new DataSet();
            adpt1.Fill(ds1, "custdata");
            if (ds1.Tables["custdata"].Rows.Count > 0)
            {

             for (int d = 0; d < DropDownList4.Items.Count; d++)
            {
               if (ds1.Tables["custdata"].Rows[0].ItemArray[7].ToString() == DropDownList4.Items[d].Text)
              {
                   DropDownList4.Items[d].Selected = true;
                   break;
               }
           }
        }

如果我清楚地理解了您的問題, ListControl.SelectedIndexChanged需要在ListControl.SelectedIndexChanged事件中使用代碼即可。

當列表控件中的選擇在服務器的帖子之間更改時發生。

<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList4_SelectedIndexChanged" ...>
</asp:DropDownList>

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{
  ...
}

正如我在評論中所寫的那樣,您應該始終在sql命令中使用參數化查詢 ,因此您的代碼已針對SQL Injection攻擊開放。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM