ListView ASP.net编辑模式 [英] ListView ASP.net Edit Mode
问题描述
当ItemUpdating事件触发时,每个人都知道如何在EditMode的Listview中获取我的文本框的新编辑值。
这是我的代码:
- 这只返回控件中的旧值bind。
< pre lang =cs> protected void lstCategory_ItemUpdating( object sender,ListViewUpdateEventArgs e)
{
// foreach(DictionaryEntry de in e.NewValues)
// {
// Response.Write(de.Value.ToString()) ;
// }
// Response.Write(进入更新);
ListViewItem itm = lstCategory.Items [e.ItemIndex] ;
TextBox tbx =(TextBox)itm.FindControl( txtCatName);
Response.Write(tbx.Text);
}
HTML代码:
asp:ListView ID =lstCategoryrunat =serverItemPlaceholderID =rowData
onitemupdating =lstCategory_ItemUpdating
onitemdeleting =lstCategory_ItemDeleting
onitemcanceling =lstCategory_ItemCanceling
onitemediting =lstCategory_ItemEditing1DataKeyNames =id
onitemcommand =lstCategory_ItemCommand>
< LayoutTemplate >
< table id = tblCategory cellpadding = 0 cellspacing = 0 >
< ; tr >
< th > 类别名称< / th >
< th > 行动< / th >
< / tr >
< tr runat = server id < span class =code-keyword> = rowData < span class =code-keyword>>
< / tr >
< / table >
< / LayoutTemplate >
< ItemTemplate >
< tr runat = server class = itm >
< < span class =code-leadattribute> td > <% #Eval( catName)%>
< / td >
< td >
< asp:LinkButton ID = lnkEdit runat = server 文字 = 编辑 CommandName = 编辑 CommandArgument =' <% #Eval( id)%> ' < span class =code-keyword> / >
< asp:LinkButton ID = lnkDelete runat = server 文本 = 删除 CommandName = 删除 CommandArgument =' <% #Eval( id)%> ' OnClientClick =' return window.confirm(您确定要删除)' / >
< / td >
< / tr >
< / ItemTemplate >
< AlternatingItemTemplate >
< tr runat = ser ver class = altitm >
< td > <% #Eval( catName)%>
< / td > ;
< td >
< asp:LinkButton ID = lnkAltEdit runat = server 文字 = 编辑 < span class =code-attribute> CommandName = 编辑 CommandArgument = ' <% #Eval( id)%> ' / >
< asp:LinkButton ID = lnkAltDelete runat = server 文字 = 删除 CommandName = 删除 CommandArgument =' <% #Eval( < span class =code-string> id)%> ' OnClientClick =' return window.confirm(您确定要删除)' / >
< / td >
< / tr >
< / AlternatingItemTemplate >
< EditItemTemplate >
< tr runat = server < span class =code-attribute> style = 背景:#778899 >
< < span class =code-leadattribute> td >
< asp:TextBox ID = txtCatName runat = server BorderColor = 黑色 BorderStyle = 实体 BorderWidth = 1px 文字 =' <% #Eval( catName)%> ' EnableViewState = true / >
< / td >
< td >
< asp:LinkButton 文字 = 保存 ID = lnkSave runat = server CommandName = 更新 / >
< asp:LinkButton 文本 < span class =code-keyword> = 取消 ID = lnkCancel runat = server CommandName = 取消 / >
< span class =code-keyword>< / td >
< / tr >
< / EditItemTemplate >
< EmptyDataTemplate >
< table >
< tr >
< > 类别名称< / th >
< th > 操作< / th >
< / tr >
< tr >
< td < span class =code-attribute> colspan = 2 >
无类别但
< / td >
< / tr >
< /表格 >
< / EmptyDataTemplate >
< / asp:ListView >
从e.NewValues字典中获取新值
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewupdateeventargs.aspx [ ^ ]
利用2路数据绑定(即要在回发中获取值,请使用Bind方法而不是Eval方法。
我已经尝试过但它不起作用。没有获得新的值。
这里是我的html标签:
< div id = content >
< asp:ListView ID = lstCategory runat = 服务器 ItemPlaceholderID = rowData
onitemupdating = lstCategory_ItemUpdating
onitemdeleting = lstCategory_ItemDeleting
onitemcanceling = lstCategory_ItemCanceling
onitemediting = lstCategory_ItemEditing1 DataKeyNames = id
onitemcommand = lstCategory_ItemCommand >
< LayoutTemplate >
< table id = tblCategory cellpadding = 0 cellspacing = 0 class = < span class =code-keyword> tblClass >
< tr >
< th class = tblClassTh > 类别名称< / th >
< th < span class =code-attribute> class = tblClassTh > 行动< / th >
< / tr >
< tr runat = serverid = rowData >
< / tr >
< / table >
< / LayoutTemplate >
< ItemTemplate >
< tr runat = 服务器 class = itm >
< td class = tblClassTd > <% #Eval( catName)%>
< / td >
< td cla ss = tblClassTd >
< asp:LinkButton ID = lnkEdit runat = server 文本 = 编辑 CommandName = 编辑 CommandArgument =' <% #Eval( id)%> ' / >
< asp:LinkButton ID = lnkDelete runat =\"server\" Text=\"Delete\" CommandName=\"Delete\" CommandArgument='<%# Eval(\"id\") %>' OnClientClick='return window.confirm(\"Are you sure to delete\")'/>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr runat=\"server\" class=\"altitm\">
<td class=\"tblClassTd\"> <%#Eval(\"catName\")%>
</td>
<td class=\"tblClassTd\">
<asp:LinkButton ID=\"lnkAltEdit\" runat=\"server \" Text=\"Edit\" CommandName=\"Edit\" CommandArgument='<%# Eval(\"id\") %>'/>
<asp:LinkButton ID=\"lnkAltDelete\" runat=\"server\" Text=\"Delete\" CommandName=\"Delete\" CommandArgument='<%# Eval(\"id\") %>' OnClientClick='return window.confirm(\"Are you sure to delete\")'/>
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr runat=\"server\" style=\"background:#778899\">
<td class=\"tblClassTd\">
<asp:TextBox ID=\"txtCatName\" runat=\"server\" BorderColor=\"Black\" BorderStyle=\"Solid\" BorderWidth=\"1px\" Text='<%#Eval(\"catName\") %>' EnableViewState =\"true\" />
</td>
<td class=\"tblClassTd\">
<asp:LinkButton Text=\"Save\" ID=\"lnkSave\" runat=\"server\" CommandName=\"Update\"/>
<asp:LinkButton Text=\"Cancel\" ID=\"lnkCancel\" runat=\"server\" CommandName=\"Cancel\"/>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table>
<tr>
<th class=\"tblClassTh\">Category Name</th>
<th class=\"tblClassTh\">Action</th>
</tr>
<tr>
<td colspan=\"2\" class=\"tblClassTd\">
No Category Yet
</td>
</tr>
</table>
</EmptyDat aTemplate>
</asp:ListView>
</div>
Hi,
Everyone knows how to get the new edited values of my textbox in Listview on EditMode when ItemUpdating Event fires.
Here is my code :
- This only return the old values bind in the control.
protected void lstCategory_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
//foreach (DictionaryEntry de in e.NewValues)
//{
// Response.Write(de.Value.ToString());
//}
//Response.Write("Getting into Update");
ListViewItem itm = lstCategory.Items[e.ItemIndex];
TextBox tbx = (TextBox)itm.FindControl("txtCatName");
Response.Write(tbx.Text);
}
HTML Code :
asp:ListView ID="lstCategory" runat="server" ItemPlaceholderID="rowData"
onitemupdating="lstCategory_ItemUpdating"
onitemdeleting="lstCategory_ItemDeleting"
onitemcanceling="lstCategory_ItemCanceling"
onitemediting="lstCategory_ItemEditing1" DataKeyNames="id"
onitemcommand="lstCategory_ItemCommand">
<LayoutTemplate>
<table id="tblCategory" cellpadding="0" cellspacing="0">
<tr>
<th>Category Name</th>
<th>Action</th>
</tr>
<tr runat="server" id="rowData">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server" class="itm">
<td><%#Eval("catName")%>
</td>
<td>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%# Eval("id") %>' />
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("id") %>' OnClientClick='return window.confirm("Are you sure to delete")'/>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr runat="server" class="altitm">
<td> <%#Eval("catName")%>
</td>
<td>
<asp:LinkButton ID="lnkAltEdit" runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%# Eval("id") %>'/>
<asp:LinkButton ID="lnkAltDelete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("id") %>' OnClientClick='return window.confirm("Are you sure to delete")'/>
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr runat="server" style="background:#778899">
<td>
<asp:TextBox ID="txtCatName" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Text='<%#Eval("catName") %>' EnableViewState ="true" />
</td>
<td>
<asp:LinkButton Text="Save" ID="lnkSave" runat="server" CommandName="Update"/>
<asp:LinkButton Text="Cancel" ID="lnkCancel" runat="server" CommandName="Cancel"/>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table>
<tr>
<th>Category Name</th>
<th>Action</th>
</tr>
<tr>
<td colspan="2">
No Category Yet
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:ListView>
Get new values from e.NewValues dictionary
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewupdateeventargs.aspx[^]
To take advantage of "2 way" databinding (i.e. to get values in the postback), use the Bind method instead of the Eval method.
I try this already but it doesn't work..No new values obtained.
here is my html tags:
<div id="content"> <asp:ListView ID="lstCategory" runat="server" ItemPlaceholderID="rowData" onitemupdating="lstCategory_ItemUpdating" onitemdeleting="lstCategory_ItemDeleting" onitemcanceling="lstCategory_ItemCanceling" onitemediting="lstCategory_ItemEditing1" DataKeyNames="id" onitemcommand="lstCategory_ItemCommand"> <LayoutTemplate> <table id="tblCategory" cellpadding="0" cellspacing="0" class="tblClass"> <tr> <th class="tblClassTh">Category Name</th> <th class="tblClassTh">Action</th> </tr> <tr runat="server" id="rowData"> </tr> </table> </LayoutTemplate> <ItemTemplate> <tr runat="server" class="itm"> <td class="tblClassTd"><%#Eval("catName")%> </td> <td class="tblClassTd"> <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%# Eval("id") %>' /> <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("id") %>' OnClientClick='return window.confirm("Are you sure to delete")'/> </td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr runat="server" class="altitm"> <td class="tblClassTd"> <%#Eval("catName")%> </td> <td class="tblClassTd"> <asp:LinkButton ID="lnkAltEdit" runat="server" Text="Edit" CommandName="Edit" CommandArgument='<%# Eval("id") %>'/> <asp:LinkButton ID="lnkAltDelete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("id") %>' OnClientClick='return window.confirm("Are you sure to delete")'/> </td> </tr> </AlternatingItemTemplate> <EditItemTemplate> <tr runat="server" style="background:#778899"> <td class="tblClassTd"> <asp:TextBox ID="txtCatName" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Text='<%#Eval("catName") %>' EnableViewState ="true" /> </td> <td class="tblClassTd"> <asp:LinkButton Text="Save" ID="lnkSave" runat="server" CommandName="Update"/> <asp:LinkButton Text="Cancel" ID="lnkCancel" runat="server" CommandName="Cancel"/> </td> </tr> </EditItemTemplate> <EmptyDataTemplate> <table> <tr> <th class="tblClassTh">Category Name</th> <th class="tblClassTh">Action</th> </tr> <tr> <td colspan="2" class="tblClassTd"> No Category Yet </td> </tr> </table> </EmptyDataTemplate> </asp:ListView> </div>
这篇关于ListView ASP.net编辑模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!