set button's commandname property as btnname,and in itemcommand event,you can write like this
if(e.commandname=="btnname")
{
......
}
if(e.commandname=="btnname")
{
......
}
解决方案 »
- 为什么http://localhost不能打开,http://127.0.0.1能够打开
- 美工帮人做一套图片要收多少钱
- 求问关于DataColumn的问题.
- 菜鸟请教:aspx如何转成html,请指点……
- 一个textBox扩张功能的疑问(类似于outlook)
- FreeTextBox
- <identity impersonate="true" userName="domain\account" password="password"/>
- 在ASP.NET的窗体中的TextBox控件能不能加上LostFocus事件
- 求助~~
- 点击asp:button怎么取得同一页面上asp:listbox选中部分的值.
- [求助] 在repeater添加了一个服务器端的CheckBox,如何知道用户选择了那行?
- 我在oracle中间了一个存储过程,返回值的类型是cursor,但是我在asp.net中不知道该如何通过这个执行存储过程得到记录集,高手们,帮帮忙
。
<asp:CheckBox ID="ckbID" CommandName="delete" runat="server"> </asp:CheckBox>应改为:
<th> <ASP:Button Text="del" CommandName="delete" runat="server" ID="btnDelete" />
。
<asp:CheckBox ID="ckbID" runat="server"> </asp:CheckBox>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.Net"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.Security " %>
<html>
<head>
<title>安泰信息网</title>
<LINK href="../css/css.css" rel="stylesheet">
<Script Language="C#" Runat="Server">
DataSet ds;
DataView dv;
FileStream fout ;
FileStream fin ;
string SortField;
string sPath;
string datafile;
ICollection CreateDataSource() {
sPath = Server.MapPath("key.xml") ;
try
{
ds = new DataSet();
fout = new FileStream(sPath,FileMode.Open, FileAccess.Read,FileShare.ReadWrite) ;
ds.ReadXml(fout, XmlReadMode.InferSchema);
fout.Close();
dv = ds.Tables[0].DefaultView;
dv.Sort = "redirect ASC";
}
catch (Exception edd)
{
Label1.Text="不能读出XML文件,因为 "+edd.ToString() ;
}
return dv;
}
public void Page_Load(Object src,EventArgs e){
if (!IsPostBack){
BindGrid();
}
CreateDataSource();
AddItem.Visible=false;
}
public void BindGrid() {
MyDataGrid.DataSource = CreateDataSource();
MyDataGrid.DataBind();
}
public void PanelShow(Object sender,EventArgs e)
{
vi.Visible = false;
AddItem.Visible = true;
Label1.Text= "";
}
public void AddItem_Click(Object sender,EventArgs e) {
datafile="key.xml";
if (redirect.Text=="")
{
Label1.Text="文件名不能为空!";
AddItem.Visible = true;
}
else {
try{
Label1.Text="" ;
fout= new FileStream(Server.MapPath(datafile),FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
DataSet ds = new DataSet();
ds.ReadXml(fout,XmlReadMode.InferSchema);
fout.Close();
DataRow newRow = ds.Tables[0].NewRow();
newRow["redirect"]=redirect.Text;
newRow["user"]=FormsAuthentication.HashPasswordForStoringInConfigFile(user.Text,"MD5");
newRow["pass"]=FormsAuthentication.HashPasswordForStoringInConfigFile(user.Text,"MD5");
ds.Tables[0].Rows.Add(newRow);
fin = new FileStream(Server.MapPath(datafile),FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
ds.WriteXml(fin, XmlWriteMode.WriteSchema);
fin.Close();
Label1.Text="";
redirect.Text="";
user.Text="";
pass.Text="";
}
catch (Exception edd)
{
Label1.Text="不能写入XML文件,因为 "+edd.ToString() ;
}
vi.Visible = true;
AddItem.Visible = false;
BindGrid();
}
}
void reset_Click(Object sender,EventArgs e)
{
redirect.Text="";
user.Text="";
pass.Text="";
vi.Visible = true;
AddItem.Visible = false;
}
public void MyDataGrid_Delete(Object sender,DataGridCommandEventArgs e)
{
if(((LinkButton)e.CommandSource).CommandName == "del")
{
int row = (int)e.Item.ItemIndex;
dv.Delete(row);
fin = new FileStream(Server.MapPath("key.xml"),FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
ds.WriteXml(fin, XmlWriteMode.WriteSchema);
fin.Close();
BindGrid();
}
}
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e) {
MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e) {
MyDataGrid.EditItemIndex = -1;
BindGrid();
}public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e) {
string redirect = e.Item.Cells[2].Text;
string user = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
string pass = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
dv.RowFilter = "redirect='"+redirect+"'";
if (dv.Count > 0) {
dv.Delete(0);
}
dv.RowFilter = "";
DataRow newRow = ds.Tables[0].NewRow();
newRow["redirect"]=redirect;
newRow["user"]=FormsAuthentication.HashPasswordForStoringInConfigFile(user,"MD5");
newRow["pass"]=FormsAuthentication.HashPasswordForStoringInConfigFile(pass,"MD5");
ds.Tables[0].Rows.Add(newRow);
fin = new FileStream(Server.MapPath("key.xml"),FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
ds.WriteXml(fin, XmlWriteMode.WriteSchema);
fin.Close();
MyDataGrid.EditItemIndex = -1;
BindGrid();
}
</script>
</head><body>
<center>
<br>
<form id="form1" runat="server">
<br>
<asp:DataGrid id="MyDataGrid" runat="server"
AllowPaging="false"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
Width="720"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
OnItemCommand="MyDataGrid_Delete"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
AutoGenerateColumns="false"
DataKeyField="redirect"
>
<columns>
<asp:ButtonColumn HeaderText="删除" Text="删除" CommandName="del" />
<asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="更新" ItemStyle-Wrap="false" HeaderText="编辑数据" HeaderStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="文件" ReadOnly="true" DataField="redirect"/>
<asp:BoundColumn HeaderText="用户名" DataField="user" ItemStyle-HorizontalAlign="center"/>
<asp:BoundColumn HeaderText="口令" DataField="pass" ItemStyle-HorizontalAlign="center"/>
</columns>
</asp:DataGrid><br><hr width="720" color="#FF9933">
<table width="720" border="0" cellspacing="0" cellpadding="1"><tr bgcolor="#aaaadd"><td align="center" valign="middle"><asp:LinkButton id="vi" Text='添加新条目' OnClick="PanelShow" runat="server"/></td></tr></table>
<asp:Panel id="AddItem" runat="server" Visible="false">
<table width="720" border="0" cellspacing="0" cellpadding="1" align="center" >
<tr bgcolor="#aaaadd">
<td width="20%" align="center">文件:</td>
<td width="80%" > <asp:TextBox id="redirect" size="10" runat="server"/></td>
</tr>
<tr>
<td width="20%" align="center">用户名:</td>
<td width="80%" colspan="3"> <asp:TextBox id="user" size="30" runat="server"/> </td>
</tr>
<tr>
<td width="20%" align="center">口令:</td>
<td width="80%" colspan="3"> <asp:TextBox id="pass" size="30" runat="server"/> </td>
</tr>
<tr >
<td colspan="4" bgcolor="#FF9933" height="2"></td>
</tr>
</table>
<asp:Button id="submit" Text="提交" OnClick="AddItem_Click" runat="server" />
<asp:Button id="reset" onClick=reset_Click runat="server" Text="取消"></asp:Button>
</asp:Panel>
<p align="center">
<asp:label id="Label1" text="" style="color:#FF0000" runat="server" />
</form>
</center>
</body>
</html>
这种情况我在DATAGRID中实现过,但是在REPEATER对XML的操作我还不是很清楚。这样的好处是在外面没有多余的东西,同时可以一次删除多个数据。另外,我想向高手请教的是:在REPEATER中加入一个控件后一些绑定问题,请赐教!!!
行 17:
<asp:repeater id="MyDataList" OnItemCommand ="delete_Row" runat="server">
这是在repeaterk中加入OnItemCommand ="delete_Row"后出现的编译错误,为什么会这样呢????
http://218.84.107.5/o.aspx
for( int i = 0; i < MyRepeater.Items.Count; i++ ){
CheckBox chk = ( CheckBox )MyRepeater.Items[i].FindControl("chk1");
if( chk.Checked )
{
int row = (int)MyRepeater.Items[i].ItemIndex;
dv.Delete(row);
fin = new FileStream(Server.MapPath("key.xml"),FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
ds.WriteXml(fin, XmlWriteMode.WriteSchema);
fin.Close();
BindRepeater();
}
}
BindRepeater();
}