簡體   English   中英

在Asp.net C#中使用dropdownList和TextBox

[英]Working with dropdownList and TextBox in Asp.net c#

在ASP.NET C#應用程序中,如何在不使用按鈕的情況下使用dropdownList或TextBox向數據庫添加值?

您需要將AutoPostBack屬性設置為true並將其保存在索引更改事件中。

<asp:DropDownList   
         ID="DropDownList1"  
         runat="server"  
         AutoPostBack="true"  
         OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"  
         >  
         <asp:ListItem>option1</asp:ListItem>  
         <asp:ListItem>option2</asp:ListItem> 

</asp:DropDownList>  
protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)  
{  
     //save data here
}  

對於下拉列表,它很簡單。 將其“ AutoPostBack”屬性設置為“ true”。 並處理“ SelectedIndexChanged”事件,如下所示。

    protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)  
    {  
      //save data here
    }

但是對於文本框,您有一個服務器端事件“ TextChanged”,也可以通過將文本框的“ AutoPostBack”設置為“ true”來使用相同的方式。

    protected void TextBox1_TextChanged(object sender, System.EventArgs e)  
    {  
      //do something here
    }

此文本框事件的缺點是每次擊鍵都會觸發該事件。

因此,對於每次擊鍵都不能很好地處理數據庫操作。

jQuery和一個隱形按鈕可以提供幫助。

有一個asp按鈕,將其可見性設置為false。 如下所示,在服務器端處理其click事件。

    protected void Button1_Click(object sender, System.EventArgs e)  
    {  
      ////save data here
    }

對於您的文本框,連接如下所示的jQuery .blur()事件並觸發其中的隱藏按鈕的click事件。

    $("#TextBox1").blur(function() {
      $("#Button1").click();
    });

當您停止編輯文本框值時,這將觸發服務器端的按鈕單擊事件。 通過服務器端的按鈕單擊事件來執行數據庫操作。

我認為您可以通過頁面加載事件來實現它

暫無
暫無

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

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