2016-11-22 77 views
0

我正在嘗試創建一個「時鐘輸入/輸出站點」,其中一個功能是用戶編輯舊時鐘輸入/輸出。我添加了一個包含2個值「IND」和「UD」的下拉菜單。我如何獲取這些值,並將它們發送給我的「CHECKIN」sql值。
我希望你知道我試圖解釋什麼。在gridview中從下拉列表中獲取數值

這是什麼樣子:

enter image description here

下拉:

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:DropDownList ID="DropDownList2" runat="server" 
      OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> 
     <asp:ListItem Selected="True">Ind</asp:ListItem> 
     <asp:ListItem Value="UD">Ud</asp:ListItem> 
     </asp:DropDownList> 
    </ItemTemplate> 
</asp:TemplateField> 

什麼香港專業教育學院的嘗試:

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    GridViewRow row = GridView1.SelectedRow; 
    string val = row.Cells[3].ToString(); 
    SqlDataSource1.SelectParameters.Add("CHECKEDIN", val); 
} 

和藏漢爲:

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DropDownList ddl = (DropDownList)GridView1.FindControl("DropDownList2"); 
    string val = ddl.SelectedValue; 
    SqlDataSource1.SelectParameters.Add("CHECKEDIN", val); 
} 

這兩個回報「空」壽

+0

Wihout嘗試它,我肯定會認爲第二個例子中,鑄塑DropDownList的,是可行的。是否有可能重新加載頁面並清除所選值(檢查頁面回發?)? –

回答

0

使用RowDataBoundGridView事件。

代碼

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow 
       && GridView1.EditIndex == e.Row.RowIndex) 
    { 
     DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList2"); 
     string val = ddl.SelectedValue.ToString(); 
     SqlDataSource1.SelectParameters.Add("CHECKEDIN", val); 
    } 
} 
+0

我沒有得到這個工作。仍然返回null –

+0

添加一個「斷點」並檢查 – Wanderer

0

的,就可以把發送回一個下拉列表,並獲得選擇的值

DropDownList dropDownList = sender as DropDownList; 
string val = dropDownList.SelectedValue; 

但你怎麼會知道用這種方法將DropDownList屬於哪一行至?

你可以使用這個技巧,就像濫用CSS類來獲得正確的行號一樣。

<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" CssClass='<%# Container.DataItemIndex %>'> 

並獲得代碼後面的行號。

int rowNumber = Convert.ToInt32(dropDownList.CssClass); 
+0

我看到你在做什麼,但我該如何實現?所有在「onselectedindexchanged」? –

相關問題