2012-01-23 62 views
2

我正在嘗試在dropdownlist的indexchange事件上爲hiddenfield賦值!其實問題是當我試圖更新我的記錄時,我無法找到隱藏字段的值!請給我解決方案或建議任何其他選項!謝謝 !在gridview中的boundfield中找不到值?

我的網格視圖是

<asp:TemplateField HeaderText="LocCode" SortExpression="LocCode"> 
    <EditItemTemplate> 
     <ajax:UpdatePanel ID="upEditsLocation" runat="server" UpdateMode="Conditional"> 
      <ContentTemplate> 
       <asp:DropDownList ID="ddlLocation" runat="server" 
       DataSourceID="sdsLocation" 
       OnDataBound="ddlLocation_DataBound" 
       DataValueField="LocCode" AppendDataBoundItems="false" 
       DataTextField="LocCode" 
       AutoPostBack="true" 
       onselectedindexchanged="ddlLocation_SelectedIndexChanged"> 
       </asp:DropDownList> 
       <asp:SqlDataSource ID="sdsLocation" runat="server" ConnectionString="<%$ ConnectionStrings:ccConnString %>" 
       ProviderName="<%$ ConnectionStrings:CCConnString.ProviderName %>" SelectCommand="Select LocCode from Location"> 
       </asp:SqlDataSource> 
      </ContentTemplate> 
     </ajax:UpdatePanel> 
    </EditItemTemplate> 
    <ItemTemplate> 
     <asp:Label ID="lblLocation" runat="server" Text='<%# Bind("LocCode") %>'> 
     </asp:Label> 
    </ItemTemplate> 
</asp:TemplateField> 

和我indexchange事件是

protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    hdloc.Value = ddlLocation.SelectedItem.Text; 

} 

我的隱藏字段是

<asp:HiddenField ID="hdloc" runat="server" /> 
+0

隱藏字段放置在哪裏?你的數據綁定控制之外? –

+0

是的!但值分配給隱藏的領域,但我不能訪問它後面的代碼!當我嘗試訪問它時,我不知道實際的問題它給null! – Chintan

+0

什麼不能訪問hdloc或ddlLocation? –

回答

0

從代碼中,我可以看到HiddenField不是一部分你的更新面板。因此,如果您爲其分配任何值,它將不會反映在客戶端計算機上。增加面板的範圍以包含隱藏字段,然後嘗試。

或者你可以從ASP.net論壇嘗試this解決方案

Here is a small tutorial on update panel (MSDN)

希望這有助於你。

+0

讓我試試這個選項! – Chintan

0
GridViewRow cancel = (GridViewRow)GridView1.Rows[e.RowIndex]; 
Label lbldeleteID = (Label)cancel.FindControl("lblid"); 
0

如果你不能從後面的代碼訪問hdloc,無論是不是由Visual Studio上aspx.designer.cs加入(嘗試刪除,並重新添加它或改變ID,然後返回到原來的值)或者隱藏字段被放置在另一個綁定控件的其他模板中,這意味着您需要使用ctrl.FindControl(「hdloc」)然後將其轉換爲HiddenField。
此外,您還需要將此隱藏字段放入UpdatePanel並使用UpdateMode =「Always」。

protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    hdloc.Value = (sender as DropDownList).SelectedItem.Text; 
} 

我敢肯定,ddlLocation.SelectedItem.Text,就像你使用它,它給一個編譯錯誤,因爲ddlLocation不上背後的代碼可見,因爲是EditItemTemplate模板內。

相關問題