datagrid中使用dropdownlist编辑模版时遇到问题
生活随笔
收集整理的這篇文章主要介紹了
datagrid中使用dropdownlist编辑模版时遇到问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前2天我做的財務(wù)管理系統(tǒng)中需要用到datagrid控件來顯示用戶信息,為了能夠編輯用戶信息,我參照微軟的asp.net入門套件ASP.NET Time Tracker Starter Kit (VBVS)中在datagrid中使用編輯模版。 在問題沒解決之前,點擊datagrid中的編輯始終報下面錯誤:
’==================
‘===================
下面是datagrid的html文件:
AllowPaging="true"?CellPadding="3"?PageSize="15"?AutoGenerateColumns="false"?EnableViewState="true"????????????OnUpdateCommand="dguserdetail_UpdateCommand"?OnEditCommand="dguserdetail_OnEdit"?OnCancelCommand="dguserdetail_OnCancel">
<AlternatingItemStyle?BackColor="#F9F9F9"></AlternatingItemStyle>
<HeaderStyle?Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateColumn?HeaderText="姓名">
<HeaderStyle?Width="100px"></HeaderStyle>
<ItemTemplate>
<asp:label?ID="username"?Text=' <%#?DataBinder.Eval(Container.DataItem,?"username").ToString()?%>?'?Runat="server"?/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn?HeaderText="密碼">
<ItemStyle?Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:label?ID="password"?Text=' <%#?DataBinder.Eval(Container.DataItem,?"password").ToString()?%>?'?Runat="server"?/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox?runat="server"?CssClass="Standard-text"?ID="txtpassword"?Text='<%#DataBinder.Eval(Container.DataItem,?"password").ToString()%>'?Width="100"?MaxLength="255">
</asp:textbox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn?HeaderText="電子郵箱">
<ItemTemplate>
<asp:label?ID="email"?Text=' <%#?DataBinder.Eval(Container.DataItem,?"email").ToString()?%>?'?Runat="server"?/>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox?Width="135px"?AutoPostBack=false?CssClass="Standard-text"?Runat="server"?ID="txtemail"?Text='<%#?DataBinder.Eval(Container.DataItem,?"email").ToString()?%>?'?/>
<asp:regularexpressionvalidator?id="RegularExpressionValidator1"?runat="server"?ErrorMessage="電子郵件無效"?ControlToValidate="txtemail"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:regularexpressionvalidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn?HeaderText="權(quán)限">
<ItemStyle?Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:label?ID="authority"?Text=' <%#?DataBinder.Eval(Container,?"DataItem.authority")?%>'?Runat="server"?/>
</ItemTemplate>
<edititemtemplate>
<asp:dropdownlist?Width="100px"?ID="DDauthority"?DataSource='<%#?bindDropDownList?%>'??Runat="server">
</asp:dropdownlist>
</edititemtemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn?ButtonType="LinkButton"?UpdateText="update"?HeaderText="編輯"?CancelText="cancel"
EditText="edit">
</asp:EditCommandColumn>
</Columns>
</asp:datagrid>
最初的頁面顯示如下:
按下編輯后的頁面:
?
對于編輯列中的edit,update,cancel需要用到下面的是件:
'==============================
dguserdetail.EditItemIndex?=?e.Item.ItemIndex?//'獲取被選中的行索引
???'Saves?current?info?on?temporary,?use?later?in?dguserdetail_Itembound()
studatagrid.username?=?CType(e.Item.FindControl("username"),?Label).Text.Trim
studatagrid.password?=?CType(e.Item.FindControl("password"),?Label).Text.Trim
studatagrid.email?=?CType(e.Item.FindControl("email"),?Label).Text.Trim
studatagrid.authority?=?CType(e.Item.FindControl("authority"),?Label).Text.Trim
'=======================================
????????SetBind()?//'最后問題解決的關(guān)鍵語句
'=======================================
?'?During?in-line-editing?mode?this?selects?corresponding?items?for?dropdowns?and?text?boxes.
'==========================================
?Private?Sub?ItemDataBound()Sub?ItemDataBound(ByVal?sender?As?Object,?_
????ByVal?e?As?DataGridItemEventArgs)?Handles?dguserdetail.ItemDataBound
????????If?e.Item.ItemType?=?ListItemType.EditItem?Then
????????????Dim?currentCbo?As?DropDownList?=?CType(e.Item.FindControl("DDauthority"),?DropDownList)
????????????currentCbo.SelectedIndex?=?currentCbo.Items.IndexOf(currentCbo.Items.FindByText(studatagrid.authority))
????????End?If
????End?Sub???????? ?'編寫從?DropDownList?中檢索當(dāng)前選定值的代碼,并執(zhí)行數(shù)據(jù)庫更新
????Protected?Sub?dguserdetail_UpdateCommand()Sub?dguserdetail_UpdateCommand(ByVal?source?As?Object,?ByVal?e?As?System.Web.UI.WebControls.DataGridCommandEventArgs)?Handles?dguserdetail.UpdateCommand
?Dim?item?As?DataGridItem?=?dguserdetail.Items(dguserdetail.EditItemIndex)
????????If?e.CommandName?=?"Update"?Then
????If?e.Item.ItemType?=?ListItemType.EditItem?Then?'只有在編輯按下以后才能提交
????????????????Dim?username?As?String
????????????????Dim?password?As?String
????????????????Dim?email?As?String
????????????????Dim?authority?As?String
????????????????Dim?authorityId?As?Integer
username?=?CType(e.Item.FindControl("username"),?Label).Text
password?=?CType(e.Item.FindControl("txtpassword"),?TextBox).Text
email?=?CType(e.Item.FindControl("txtemail"),?TextBox).Text
authority?=?CType(e.Item.FindControl("ddauthority"),?DropDownList).SelectedItem.Value
authorityId?=?IIf(authority?=?"系統(tǒng)管理員",?1,?0)
'?Save?the?dguserdetail?object.
????????????Try
?????SqlHelper.ExecuteNonQuery(strconn,?"updateuser",?username,?password,?email,?authorityId)
????????????Catch
????????????End?Try
End?If
'?Quit?in-line-editing?mode.
???dguserdetail.EditItemIndex?=?-1
???SetBind()
End?If
End?Sub
????Protected?Sub?dguserdetail_OnCancel()Sub?dguserdetail_OnCancel(ByVal?sender?As?[Object],?ByVal?e?As?DataGridCommandEventArgs)?Handles?dguserdetail.CancelCommand
????????dguserdetail.EditItemIndex?=?-1
????????SetBind()
????End?Sub?'TimeEntryGrid_OnCancel
轉(zhuǎn)載于:https://www.cnblogs.com/lvxuehui/archive/2005/04/30/148263.html
總結(jié)
以上是生活随笔為你收集整理的datagrid中使用dropdownlist编辑模版时遇到问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浙大课程
- 下一篇: 在ASP.Net中两种利用CSS实现多界