if(e.CommandName == "EditOrder"){id=已经取到一个值}
在编辑完成后时候有个按钮,有一个更新的语句,要取到这里的id,
在编辑的这个确认按钮中,问如何取到这个id的值。(我用的不是gridview自带的更新)
27 个解决方案
#1
按钮是在Button1_Click的事件中
#2
把你放按钮的列,改成模版列。
然后可以设置这个按钮的CommandName、CommandArgument,改值可以绑定字段。
然后可以设置这个按钮的CommandName、CommandArgument,改值可以绑定字段。
#3
具体给我说下好吗,就一点点代码,更新时where后面的条件
#4
设置CommandArgument='<%# Eval("id")%>'
#5
这个我有,有没有where后面条件。where id=?,问号是什么。怎么取id
#6
sql:id=@id,@id sqlparameter
#7
不是很懂,我才学半个月,下面是我的代码帮我补全,谢谢大神啊。
protected void Button7_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Teams set TeamNo=@TeamNo,TeamName=@TeamName,TeamStrength=@TeamStrength,TeamLeader=@TeamLeader,TeamPoints=@TeamPoints,TeamAverage=@TeamAverage where TeamNo='" + , con);
//上面是.cs中的,下面是aspx中的,帮我补全where后面具体怎么写,要编辑的时候,每行后面的LinkButton怎么用。
<Columns>
<asp:BoundField DataField="TeamNo" HeaderText="团队编号" SortExpression="TeamNo"/>
<asp:BoundField DataField="TeamName" HeaderText="团队名称" SortExpression="TeamName"/>
<asp:BoundField DataField="TeamStrength" HeaderText="团队人数" SortExpression="TeamStrength"/>
<asp:BoundField DataField="TeamLeader" HeaderText="团队负责人" SortExpression="TeamLeader"/>
<asp:BoundField DataField="TeamPoints" HeaderText="团队积分" SortExpression="TeamPoints"/>
<asp:BoundField DataField="TeamAverage" HeaderText="团队平均积分" SortExpression="TeamAverage"/>
<asp:TemplateField HeaderText="编辑" >
<ItemTemplate>
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' />
</ItemTemplate>
</asp:TemplateField>
#8
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
div1.Visible = false;
div2.Visible = true;
con.Open();
int id = Convert.ToInt32(e.CommandArgument.ToString());
//这个是取到的id
#9
你给 GridView 设置上 DataKeyString 然后 你通过
GridView的编辑事件。里 e.Index 去 DataKeys[] 就可以得到你要的数据了
GridView的编辑事件。里 e.Index 去 DataKeys[] 就可以得到你要的数据了
#10
你设置Linkbutton的CommandArgument是你要抓的id,你执行sql的button是在按下linkbutton之后吗?
如果是的话,建议你增加隐藏控件记录id的值,这样每次按下linkbutton这个隐藏控件的value就是你要抓的id,执行sql的button只要抓隐藏控件的值就OK
如果是的话,建议你增加隐藏控件记录id的值,这样每次按下linkbutton这个隐藏控件的value就是你要抓的id,执行sql的button只要抓隐藏控件的值就OK
#11
LinkButton lb = sender as LinkButton;
string id = lb.CommandArgument.Tostring();
楼主试下这个,我以前写过,你看这个对不,不对在问
string id = lb.CommandArgument.Tostring();
楼主试下这个,我以前写过,你看这个对不,不对在问
#12
<asp:LinkButton
ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" OnCommand="SetMarried" CommandArgument='<%# Eval("TeamNo") %>'
定义一个全局变量 public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
temno=convert.toint32( e.CommandArgument.ToString());//强制转换
}
temno这样就可以在别的地方用了。你的where之类的
ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" OnCommand="SetMarried" CommandArgument='<%# Eval("TeamNo") %>'
定义一个全局变量 public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
temno=convert.toint32( e.CommandArgument.ToString());//强制转换
}
temno这样就可以在别的地方用了。你的where之类的
#13
有代码了,你可以看看GridView DataKeyName="TeamNo",弄不出来啊,急
#14
不行额,这样编辑都错了。
#15
谢谢,你的方法好像可以,但是为什么不能编辑呢,编辑时TeamNo不改,下面会说重复,这个不是更新吗,怎么会说重复,确定数据库中没有相同数据了。下面给了编辑的div,这是它的确定按钮事件。GridView1中间也有个编辑按钮,跟上面GridView2给的代码一样
public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换
}
protected void Button7_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Teams set TeamNo=@TeamNo,TeamName=@TeamName,TeamStrength=@TeamStrength,TeamLeader=@TeamLeader,TeamPoints=@TeamPoints,TeamAverage=@TeamAverage where TeamNo="+teamno , con);
cmd.Parameters.Add("@TeamNo", SqlDbType.Int);
cmd.Parameters.Add("@TeamName", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamStrength", SqlDbType.Int);
cmd.Parameters.Add("@TeamLeader", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamPoints", SqlDbType.Int);
cmd.Parameters.Add("@TeamAverage", SqlDbType.Decimal);
//SqlDataAdapter myda=new SqlDataAdapter();
//myda.UpdateCommand=cmd;
//myda.Update()
String sql1 = "select TeamNo from Teams where TeamNo=" + TextBox11.Text.Trim();
String sql2 = "select TeamName from Teams where TeamNo=" + TextBox12.Text.Trim();
SqlCommand cmd1 = new SqlCommand(sql1, con);
SqlCommand cmd2 = new SqlCommand(sql2, con);
SqlDataReader sdr1 = cmd1.ExecuteReader();
sdr1.Read();
if (sdr1.HasRows)
{
Response.Write("<script>alert('已存在该团队编号,请您重新输入!')</script>");
sdr1.Close();
con.Close();
}
else
{
sdr1.Close();
SqlDataReader sdr2 = cmd2.ExecuteReader();
sdr2.Read();
if (sdr2.HasRows)
{
Response.Write("<script>alert('已存在该团队名称,请您重新输入!')</script>");
sdr2.Close();
con.Close();
}
else
{
cmd.Parameters["@TeamNo"].Value = TextBox11.Text.Trim();
cmd.Parameters["@TeamName"].Value = TextBox12.Text.Trim();
cmd.Parameters["@TeamStrength"].Value = TextBox13.Text.Trim();
cmd.Parameters["@TeamLeader"].Value = TextBox14.Text.Trim();
cmd.Parameters["@TeamPoints"].Value = TextBox15.Text.Trim();
cmd.Parameters["@TeamAverage"].Value = TextBox16.Text.Trim();
sdr2.Close();
cmd.ExecuteNonQuery();
con.Close();
init();
div2.Visible = false;
}
}
bind();
}
#16
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换 设置断点跟着看看。是不是值一直不变还是。选中这行 按F9
#17
前台这样绑定:
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' />
</ItemTemplate>
================
后台这样取值:
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
div1.……
是正确的啊,不知道你想干什么?还有什么问题?
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' />
</ItemTemplate>
================
后台这样取值:
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
div1.……
是正确的啊,不知道你想干什么?还有什么问题?
#18
运行效果一样啊,跟没断点没区别,别让我用断点啊,我感觉你的那个没取到数值原因,因为数据不改按确认,还是会提示重复的,应该是没有取到teamno。
急急急。上面代码你看看呢还是还需要什么代码
#19
<asp:GridView ID="dvlist" runat="server" AutoGenerateColumns="False" OnRowCommand="dvlist_RowCommand">
<Columns>
<asp:BoundField DataField="username" HeaderText="用户名" />
<asp:BoundField DataField="pass" HeaderText="pass" />
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" Text="编辑" CommandName="UserUpdate" CommandArgument='<%# Eval("id") %>' />
<asp:Button ID="BtnDelete" runat="server" Text="删除" CommandName="UserDelete" CommandArgument='<%# Eval("username") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void bind()
{
DataTable dt = WebCommon.DBObject.getData("select * from dbo.userinfo").Tables[0];
this.dvlist.DataSource = dt;
this.dvlist.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
bind();
}
protected void dvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UserUpdate")
{
Response.Write(e.CommandArgument.ToString());
}
if (e.CommandName == "UserDelete")
{
Response.Write(e.CommandArgument.ToString());
}
}
<Columns>
<asp:BoundField DataField="username" HeaderText="用户名" />
<asp:BoundField DataField="pass" HeaderText="pass" />
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" Text="编辑" CommandName="UserUpdate" CommandArgument='<%# Eval("id") %>' />
<asp:Button ID="BtnDelete" runat="server" Text="删除" CommandName="UserDelete" CommandArgument='<%# Eval("username") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void bind()
{
DataTable dt = WebCommon.DBObject.getData("select * from dbo.userinfo").Tables[0];
this.dvlist.DataSource = dt;
this.dvlist.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
bind();
}
protected void dvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UserUpdate")
{
Response.Write(e.CommandArgument.ToString());
}
if (e.CommandName == "UserDelete")
{
Response.Write(e.CommandArgument.ToString());
}
}
#20
是正确的啊,但是我在(请看15楼代码,中我要去到teamno的值取不到,编辑没用,在button,我是要取到那个值,15楼的button事件上面的方法如何改进)
前台还有个 oncommand="SetMarried"
后台
public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换
}
但是取不到teamno,帮我看看呗,反正是不能更新,还需要什么代码说
#21
客戶端增加代碼 <input id ="IDTEXT" runat= "server" value ="" visible="false"/>
<input id ="IDTEXT2" runat= "server" value ="" visible="false"/>
protected void dvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UserUpdate")
{
IDTEXT.Value = e.CommandArgument.ToString();
Response.Write(e.CommandArgument.ToString());
}
if (e.CommandName == "UserDelete")
{
IDTEXT2.Value = e.CommandArgument.ToString();
Response.Write(e.CommandArgument.ToString());
}
}
其他需要抓值的地方直接用IDTEXT2.Value 就可以了,根據情況在合適的地方清空隱藏變量的值
在需要用之前要判斷是否為空,因為你沒有點grid中的button這隱藏控件是沒有值的
<input id ="IDTEXT2" runat= "server" value ="" visible="false"/>
protected void dvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UserUpdate")
{
IDTEXT.Value = e.CommandArgument.ToString();
Response.Write(e.CommandArgument.ToString());
}
if (e.CommandName == "UserDelete")
{
IDTEXT2.Value = e.CommandArgument.ToString();
Response.Write(e.CommandArgument.ToString());
}
}
其他需要抓值的地方直接用IDTEXT2.Value 就可以了,根據情況在合適的地方清空隱藏變量的值
在需要用之前要判斷是否為空,因為你沒有點grid中的button這隱藏控件是沒有值的
#22
说明运行界面在我的相册中,这个最好去看看,注意我的编辑按钮用的是LinkButton不是GridView自带的。
我要取出的是button7_click事件中的teamno的值,下面给出具体代码:
前台主要代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="9pt" OnRowDeleting="GridView1_RowDeleting"
OnSorting="GridView1_Sorting" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
AllowPaging="True" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" AllowSorting="True" DataKeyNames="TeamNo">
<Columns>
<asp:BoundField DataField="TeamNo" HeaderText="团队编号" SortExpression="TeamNo"/>
<asp:BoundField DataField="TeamName" HeaderText="团队名称" SortExpression="TeamName"/>
<asp:BoundField DataField="TeamStrength" HeaderText="团队人数" SortExpression="TeamStrength"/>
<asp:BoundField DataField="TeamLeader" HeaderText="团队负责人" SortExpression="TeamLeader"/>
<asp:BoundField DataField="TeamPoints" HeaderText="团队积分" SortExpression="TeamPoints"/>
<asp:BoundField DataField="TeamAverage" HeaderText="团队平均积分" SortExpression="TeamAverage"/>
<asp:TemplateField HeaderText="编辑" >
<ItemTemplate>
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' OnCommand="SetMarried"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
后台主要代码
public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换
}//这里是根据上面的写的,取不到teamno,因此修改不成功
protected void Button7_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Teams set TeamNo=@TeamNo,TeamName=@TeamName,TeamStrength=@TeamStrength,TeamLeader=@TeamLeader,TeamPoints=@TeamPoints,TeamAverage=@TeamAverage where TeamNo="+ teamno , con);// 我要取的就是这个teamno,这里是关键 cmd.Parameters.Add("@TeamNo", SqlDbType.Int);
cmd.Parameters.Add("@TeamName", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamStrength", SqlDbType.Int);
cmd.Parameters.Add("@TeamLeader", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamPoints", SqlDbType.Int);
cmd.Parameters.Add("@TeamAverage", SqlDbType.Decimal);
//SqlDataAdapter myda=new SqlDataAdapter();
//myda.UpdateCommand=cmd;
//myda.Update()
String sql1 = "select TeamNo from Teams where TeamNo=" + TextBox11.Text.Trim();
String sql2 = "select TeamName from Teams where TeamNo=" + TextBox12.Text.Trim();
SqlCommand cmd1 = new SqlCommand(sql1, con);
SqlCommand cmd2 = new SqlCommand(sql2, con);
SqlDataReader sdr1 = cmd1.ExecuteReader();
sdr1.Read();
if (sdr1.HasRows)
{
Response.Write("<script>alert('已存在该团队编号,请您重新输入!')</script>");
sdr1.Close();
con.Close();
}
else
{
sdr1.Close();
SqlDataReader sdr2 = cmd2.ExecuteReader();
sdr2.Read();
if (sdr2.HasRows)
{
Response.Write("<script>alert('已存在该团队名称,请您重新输入!')</script>");
sdr2.Close();
con.Close();
}
else
{
cmd.Parameters["@TeamNo"].Value = TextBox11.Text.Trim();
cmd.Parameters["@TeamName"].Value = TextBox12.Text.Trim();
cmd.Parameters["@TeamStrength"].Value = TextBox13.Text.Trim();
cmd.Parameters["@TeamLeader"].Value = TextBox14.Text.Trim();
cmd.Parameters["@TeamPoints"].Value = TextBox15.Text.Trim();
cmd.Parameters["@TeamAverage"].Value = TextBox16.Text.Trim();
sdr2.Close();
cmd.ExecuteNonQuery();
con.Close();
init();
div2.Visible = false;
}
}
bind();
}
这里是编辑按钮的id获得方法,就是这个id的值怎么能取到呢,这里的id跟teamno息息相关的。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
con.Open();
int id = Convert.ToInt32(e.CommandArgument.ToString());
前台主要代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="9pt" OnRowDeleting="GridView1_RowDeleting"
OnSorting="GridView1_Sorting" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
AllowPaging="True" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" AllowSorting="True" DataKeyNames="TeamNo">
<Columns>
<asp:BoundField DataField="TeamNo" HeaderText="团队编号" SortExpression="TeamNo"/>
<asp:BoundField DataField="TeamName" HeaderText="团队名称" SortExpression="TeamName"/>
<asp:BoundField DataField="TeamStrength" HeaderText="团队人数" SortExpression="TeamStrength"/>
<asp:BoundField DataField="TeamLeader" HeaderText="团队负责人" SortExpression="TeamLeader"/>
<asp:BoundField DataField="TeamPoints" HeaderText="团队积分" SortExpression="TeamPoints"/>
<asp:BoundField DataField="TeamAverage" HeaderText="团队平均积分" SortExpression="TeamAverage"/>
<asp:TemplateField HeaderText="编辑" >
<ItemTemplate>
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' OnCommand="SetMarried"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
后台主要代码
public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换
}//这里是根据上面的写的,取不到teamno,因此修改不成功
protected void Button7_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Teams set TeamNo=@TeamNo,TeamName=@TeamName,TeamStrength=@TeamStrength,TeamLeader=@TeamLeader,TeamPoints=@TeamPoints,TeamAverage=@TeamAverage where TeamNo="+ teamno , con);// 我要取的就是这个teamno,这里是关键 cmd.Parameters.Add("@TeamNo", SqlDbType.Int);
cmd.Parameters.Add("@TeamName", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamStrength", SqlDbType.Int);
cmd.Parameters.Add("@TeamLeader", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamPoints", SqlDbType.Int);
cmd.Parameters.Add("@TeamAverage", SqlDbType.Decimal);
//SqlDataAdapter myda=new SqlDataAdapter();
//myda.UpdateCommand=cmd;
//myda.Update()
String sql1 = "select TeamNo from Teams where TeamNo=" + TextBox11.Text.Trim();
String sql2 = "select TeamName from Teams where TeamNo=" + TextBox12.Text.Trim();
SqlCommand cmd1 = new SqlCommand(sql1, con);
SqlCommand cmd2 = new SqlCommand(sql2, con);
SqlDataReader sdr1 = cmd1.ExecuteReader();
sdr1.Read();
if (sdr1.HasRows)
{
Response.Write("<script>alert('已存在该团队编号,请您重新输入!')</script>");
sdr1.Close();
con.Close();
}
else
{
sdr1.Close();
SqlDataReader sdr2 = cmd2.ExecuteReader();
sdr2.Read();
if (sdr2.HasRows)
{
Response.Write("<script>alert('已存在该团队名称,请您重新输入!')</script>");
sdr2.Close();
con.Close();
}
else
{
cmd.Parameters["@TeamNo"].Value = TextBox11.Text.Trim();
cmd.Parameters["@TeamName"].Value = TextBox12.Text.Trim();
cmd.Parameters["@TeamStrength"].Value = TextBox13.Text.Trim();
cmd.Parameters["@TeamLeader"].Value = TextBox14.Text.Trim();
cmd.Parameters["@TeamPoints"].Value = TextBox15.Text.Trim();
cmd.Parameters["@TeamAverage"].Value = TextBox16.Text.Trim();
sdr2.Close();
cmd.ExecuteNonQuery();
con.Close();
init();
div2.Visible = false;
}
}
bind();
}
这里是编辑按钮的id获得方法,就是这个id的值怎么能取到呢,这里的id跟teamno息息相关的。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
con.Open();
int id = Convert.ToInt32(e.CommandArgument.ToString());
#23
#24
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
con.Open();
Button7.CommandArgument=e.CommandArgument;//这里把linkbutton的参数赋值给button7
.....
}
protected void Button7_Click(object sender, EventArgs e)
{
string id = (sender as Button).CommandArgument.ToString();
//或者string id = button7.CommandArgument.ToString();
}
上面的方式是在GridView1_RowCommand把CommandArgument传递给按钮,这样button7事件中也可以获取到该CommandArgument了。
或者也可以使用另外一种方式:
在函数之外定义一个变量,在GridView1_RowCommand中把CommandArgument的值给变量。在button7事件中取该变量的值。
{
if (e.CommandName == "EditOrder")
{
con.Open();
Button7.CommandArgument=e.CommandArgument;//这里把linkbutton的参数赋值给button7
.....
}
protected void Button7_Click(object sender, EventArgs e)
{
string id = (sender as Button).CommandArgument.ToString();
//或者string id = button7.CommandArgument.ToString();
}
上面的方式是在GridView1_RowCommand把CommandArgument传递给按钮,这样button7事件中也可以获取到该CommandArgument了。
或者也可以使用另外一种方式:
在函数之外定义一个变量,在GridView1_RowCommand中把CommandArgument的值给变量。在button7事件中取该变量的值。
#25
错误 2 无法将类型“object”隐式转换为“string”。存在一个显式转换(是否缺少强制转换?) d:\我的文档\Visual Studio 2005\WebSites\WebSite1\TeamManage.aspx.cs 305 39 d:\...\WebSite1\
//这个是在rowcommand中提示的(Button7.CommandArgument=e.CommandArgument;)初学半个月,你说方法有些能理解,但是我不会实现。
#26
#27
GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被选中的索引值
在linkbutton控件的Command事件,利用sender的Parent获取GridView中的当前行。
protected void lbtnQianChu_Command(object sender, CommandEventArgs e)
{
LinkButton lb = (LinkButton)sender;
DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
GridViewRow gvr = (GridViewRow)dcf.Parent; //此得出的值是表示那行被选中的索引值
}
LinkButton获取当前行或id办法http://ch-kexin.iteye.com/blog/566942
GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被选中的索引值
在linkbutton控件的Command事件,利用sender的Parent获取GridView中的当前行。
protected void lbtnQianChu_Command(object sender, CommandEventArgs e)
{
LinkButton lb = (LinkButton)sender;
DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
GridViewRow gvr = (GridViewRow)dcf.Parent; //此得出的值是表示那行被选中的索引值
}
LinkButton获取当前行或id办法http://ch-kexin.iteye.com/blog/566942
#1
按钮是在Button1_Click的事件中
#2
把你放按钮的列,改成模版列。
然后可以设置这个按钮的CommandName、CommandArgument,改值可以绑定字段。
然后可以设置这个按钮的CommandName、CommandArgument,改值可以绑定字段。
#3
具体给我说下好吗,就一点点代码,更新时where后面的条件
#4
设置CommandArgument='<%# Eval("id")%>'
#5
这个我有,有没有where后面条件。where id=?,问号是什么。怎么取id
#6
sql:id=@id,@id sqlparameter
#7
不是很懂,我才学半个月,下面是我的代码帮我补全,谢谢大神啊。
protected void Button7_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Teams set TeamNo=@TeamNo,TeamName=@TeamName,TeamStrength=@TeamStrength,TeamLeader=@TeamLeader,TeamPoints=@TeamPoints,TeamAverage=@TeamAverage where TeamNo='" + , con);
//上面是.cs中的,下面是aspx中的,帮我补全where后面具体怎么写,要编辑的时候,每行后面的LinkButton怎么用。
<Columns>
<asp:BoundField DataField="TeamNo" HeaderText="团队编号" SortExpression="TeamNo"/>
<asp:BoundField DataField="TeamName" HeaderText="团队名称" SortExpression="TeamName"/>
<asp:BoundField DataField="TeamStrength" HeaderText="团队人数" SortExpression="TeamStrength"/>
<asp:BoundField DataField="TeamLeader" HeaderText="团队负责人" SortExpression="TeamLeader"/>
<asp:BoundField DataField="TeamPoints" HeaderText="团队积分" SortExpression="TeamPoints"/>
<asp:BoundField DataField="TeamAverage" HeaderText="团队平均积分" SortExpression="TeamAverage"/>
<asp:TemplateField HeaderText="编辑" >
<ItemTemplate>
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' />
</ItemTemplate>
</asp:TemplateField>
#8
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
div1.Visible = false;
div2.Visible = true;
con.Open();
int id = Convert.ToInt32(e.CommandArgument.ToString());
//这个是取到的id
#9
你给 GridView 设置上 DataKeyString 然后 你通过
GridView的编辑事件。里 e.Index 去 DataKeys[] 就可以得到你要的数据了
GridView的编辑事件。里 e.Index 去 DataKeys[] 就可以得到你要的数据了
#10
你设置Linkbutton的CommandArgument是你要抓的id,你执行sql的button是在按下linkbutton之后吗?
如果是的话,建议你增加隐藏控件记录id的值,这样每次按下linkbutton这个隐藏控件的value就是你要抓的id,执行sql的button只要抓隐藏控件的值就OK
如果是的话,建议你增加隐藏控件记录id的值,这样每次按下linkbutton这个隐藏控件的value就是你要抓的id,执行sql的button只要抓隐藏控件的值就OK
#11
LinkButton lb = sender as LinkButton;
string id = lb.CommandArgument.Tostring();
楼主试下这个,我以前写过,你看这个对不,不对在问
string id = lb.CommandArgument.Tostring();
楼主试下这个,我以前写过,你看这个对不,不对在问
#12
<asp:LinkButton
ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" OnCommand="SetMarried" CommandArgument='<%# Eval("TeamNo") %>'
定义一个全局变量 public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
temno=convert.toint32( e.CommandArgument.ToString());//强制转换
}
temno这样就可以在别的地方用了。你的where之类的
ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" OnCommand="SetMarried" CommandArgument='<%# Eval("TeamNo") %>'
定义一个全局变量 public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
temno=convert.toint32( e.CommandArgument.ToString());//强制转换
}
temno这样就可以在别的地方用了。你的where之类的
#13
有代码了,你可以看看GridView DataKeyName="TeamNo",弄不出来啊,急
#14
不行额,这样编辑都错了。
#15
谢谢,你的方法好像可以,但是为什么不能编辑呢,编辑时TeamNo不改,下面会说重复,这个不是更新吗,怎么会说重复,确定数据库中没有相同数据了。下面给了编辑的div,这是它的确定按钮事件。GridView1中间也有个编辑按钮,跟上面GridView2给的代码一样
public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换
}
protected void Button7_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Teams set TeamNo=@TeamNo,TeamName=@TeamName,TeamStrength=@TeamStrength,TeamLeader=@TeamLeader,TeamPoints=@TeamPoints,TeamAverage=@TeamAverage where TeamNo="+teamno , con);
cmd.Parameters.Add("@TeamNo", SqlDbType.Int);
cmd.Parameters.Add("@TeamName", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamStrength", SqlDbType.Int);
cmd.Parameters.Add("@TeamLeader", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamPoints", SqlDbType.Int);
cmd.Parameters.Add("@TeamAverage", SqlDbType.Decimal);
//SqlDataAdapter myda=new SqlDataAdapter();
//myda.UpdateCommand=cmd;
//myda.Update()
String sql1 = "select TeamNo from Teams where TeamNo=" + TextBox11.Text.Trim();
String sql2 = "select TeamName from Teams where TeamNo=" + TextBox12.Text.Trim();
SqlCommand cmd1 = new SqlCommand(sql1, con);
SqlCommand cmd2 = new SqlCommand(sql2, con);
SqlDataReader sdr1 = cmd1.ExecuteReader();
sdr1.Read();
if (sdr1.HasRows)
{
Response.Write("<script>alert('已存在该团队编号,请您重新输入!')</script>");
sdr1.Close();
con.Close();
}
else
{
sdr1.Close();
SqlDataReader sdr2 = cmd2.ExecuteReader();
sdr2.Read();
if (sdr2.HasRows)
{
Response.Write("<script>alert('已存在该团队名称,请您重新输入!')</script>");
sdr2.Close();
con.Close();
}
else
{
cmd.Parameters["@TeamNo"].Value = TextBox11.Text.Trim();
cmd.Parameters["@TeamName"].Value = TextBox12.Text.Trim();
cmd.Parameters["@TeamStrength"].Value = TextBox13.Text.Trim();
cmd.Parameters["@TeamLeader"].Value = TextBox14.Text.Trim();
cmd.Parameters["@TeamPoints"].Value = TextBox15.Text.Trim();
cmd.Parameters["@TeamAverage"].Value = TextBox16.Text.Trim();
sdr2.Close();
cmd.ExecuteNonQuery();
con.Close();
init();
div2.Visible = false;
}
}
bind();
}
#16
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换 设置断点跟着看看。是不是值一直不变还是。选中这行 按F9
#17
前台这样绑定:
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' />
</ItemTemplate>
================
后台这样取值:
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
div1.……
是正确的啊,不知道你想干什么?还有什么问题?
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' />
</ItemTemplate>
================
后台这样取值:
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
div1.……
是正确的啊,不知道你想干什么?还有什么问题?
#18
运行效果一样啊,跟没断点没区别,别让我用断点啊,我感觉你的那个没取到数值原因,因为数据不改按确认,还是会提示重复的,应该是没有取到teamno。
急急急。上面代码你看看呢还是还需要什么代码
#19
<asp:GridView ID="dvlist" runat="server" AutoGenerateColumns="False" OnRowCommand="dvlist_RowCommand">
<Columns>
<asp:BoundField DataField="username" HeaderText="用户名" />
<asp:BoundField DataField="pass" HeaderText="pass" />
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" Text="编辑" CommandName="UserUpdate" CommandArgument='<%# Eval("id") %>' />
<asp:Button ID="BtnDelete" runat="server" Text="删除" CommandName="UserDelete" CommandArgument='<%# Eval("username") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void bind()
{
DataTable dt = WebCommon.DBObject.getData("select * from dbo.userinfo").Tables[0];
this.dvlist.DataSource = dt;
this.dvlist.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
bind();
}
protected void dvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UserUpdate")
{
Response.Write(e.CommandArgument.ToString());
}
if (e.CommandName == "UserDelete")
{
Response.Write(e.CommandArgument.ToString());
}
}
<Columns>
<asp:BoundField DataField="username" HeaderText="用户名" />
<asp:BoundField DataField="pass" HeaderText="pass" />
<asp:TemplateField HeaderText="编辑">
<ItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" Text="编辑" CommandName="UserUpdate" CommandArgument='<%# Eval("id") %>' />
<asp:Button ID="BtnDelete" runat="server" Text="删除" CommandName="UserDelete" CommandArgument='<%# Eval("username") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void bind()
{
DataTable dt = WebCommon.DBObject.getData("select * from dbo.userinfo").Tables[0];
this.dvlist.DataSource = dt;
this.dvlist.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
bind();
}
protected void dvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UserUpdate")
{
Response.Write(e.CommandArgument.ToString());
}
if (e.CommandName == "UserDelete")
{
Response.Write(e.CommandArgument.ToString());
}
}
#20
是正确的啊,但是我在(请看15楼代码,中我要去到teamno的值取不到,编辑没用,在button,我是要取到那个值,15楼的button事件上面的方法如何改进)
前台还有个 oncommand="SetMarried"
后台
public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换
}
但是取不到teamno,帮我看看呗,反正是不能更新,还需要什么代码说
#21
客戶端增加代碼 <input id ="IDTEXT" runat= "server" value ="" visible="false"/>
<input id ="IDTEXT2" runat= "server" value ="" visible="false"/>
protected void dvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UserUpdate")
{
IDTEXT.Value = e.CommandArgument.ToString();
Response.Write(e.CommandArgument.ToString());
}
if (e.CommandName == "UserDelete")
{
IDTEXT2.Value = e.CommandArgument.ToString();
Response.Write(e.CommandArgument.ToString());
}
}
其他需要抓值的地方直接用IDTEXT2.Value 就可以了,根據情況在合適的地方清空隱藏變量的值
在需要用之前要判斷是否為空,因為你沒有點grid中的button這隱藏控件是沒有值的
<input id ="IDTEXT2" runat= "server" value ="" visible="false"/>
protected void dvlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "UserUpdate")
{
IDTEXT.Value = e.CommandArgument.ToString();
Response.Write(e.CommandArgument.ToString());
}
if (e.CommandName == "UserDelete")
{
IDTEXT2.Value = e.CommandArgument.ToString();
Response.Write(e.CommandArgument.ToString());
}
}
其他需要抓值的地方直接用IDTEXT2.Value 就可以了,根據情況在合適的地方清空隱藏變量的值
在需要用之前要判斷是否為空,因為你沒有點grid中的button這隱藏控件是沒有值的
#22
说明运行界面在我的相册中,这个最好去看看,注意我的编辑按钮用的是LinkButton不是GridView自带的。
我要取出的是button7_click事件中的teamno的值,下面给出具体代码:
前台主要代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="9pt" OnRowDeleting="GridView1_RowDeleting"
OnSorting="GridView1_Sorting" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
AllowPaging="True" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" AllowSorting="True" DataKeyNames="TeamNo">
<Columns>
<asp:BoundField DataField="TeamNo" HeaderText="团队编号" SortExpression="TeamNo"/>
<asp:BoundField DataField="TeamName" HeaderText="团队名称" SortExpression="TeamName"/>
<asp:BoundField DataField="TeamStrength" HeaderText="团队人数" SortExpression="TeamStrength"/>
<asp:BoundField DataField="TeamLeader" HeaderText="团队负责人" SortExpression="TeamLeader"/>
<asp:BoundField DataField="TeamPoints" HeaderText="团队积分" SortExpression="TeamPoints"/>
<asp:BoundField DataField="TeamAverage" HeaderText="团队平均积分" SortExpression="TeamAverage"/>
<asp:TemplateField HeaderText="编辑" >
<ItemTemplate>
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' OnCommand="SetMarried"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
后台主要代码
public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换
}//这里是根据上面的写的,取不到teamno,因此修改不成功
protected void Button7_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Teams set TeamNo=@TeamNo,TeamName=@TeamName,TeamStrength=@TeamStrength,TeamLeader=@TeamLeader,TeamPoints=@TeamPoints,TeamAverage=@TeamAverage where TeamNo="+ teamno , con);// 我要取的就是这个teamno,这里是关键 cmd.Parameters.Add("@TeamNo", SqlDbType.Int);
cmd.Parameters.Add("@TeamName", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamStrength", SqlDbType.Int);
cmd.Parameters.Add("@TeamLeader", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamPoints", SqlDbType.Int);
cmd.Parameters.Add("@TeamAverage", SqlDbType.Decimal);
//SqlDataAdapter myda=new SqlDataAdapter();
//myda.UpdateCommand=cmd;
//myda.Update()
String sql1 = "select TeamNo from Teams where TeamNo=" + TextBox11.Text.Trim();
String sql2 = "select TeamName from Teams where TeamNo=" + TextBox12.Text.Trim();
SqlCommand cmd1 = new SqlCommand(sql1, con);
SqlCommand cmd2 = new SqlCommand(sql2, con);
SqlDataReader sdr1 = cmd1.ExecuteReader();
sdr1.Read();
if (sdr1.HasRows)
{
Response.Write("<script>alert('已存在该团队编号,请您重新输入!')</script>");
sdr1.Close();
con.Close();
}
else
{
sdr1.Close();
SqlDataReader sdr2 = cmd2.ExecuteReader();
sdr2.Read();
if (sdr2.HasRows)
{
Response.Write("<script>alert('已存在该团队名称,请您重新输入!')</script>");
sdr2.Close();
con.Close();
}
else
{
cmd.Parameters["@TeamNo"].Value = TextBox11.Text.Trim();
cmd.Parameters["@TeamName"].Value = TextBox12.Text.Trim();
cmd.Parameters["@TeamStrength"].Value = TextBox13.Text.Trim();
cmd.Parameters["@TeamLeader"].Value = TextBox14.Text.Trim();
cmd.Parameters["@TeamPoints"].Value = TextBox15.Text.Trim();
cmd.Parameters["@TeamAverage"].Value = TextBox16.Text.Trim();
sdr2.Close();
cmd.ExecuteNonQuery();
con.Close();
init();
div2.Visible = false;
}
}
bind();
}
这里是编辑按钮的id获得方法,就是这个id的值怎么能取到呢,这里的id跟teamno息息相关的。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
con.Open();
int id = Convert.ToInt32(e.CommandArgument.ToString());
前台主要代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Size="9pt" OnRowDeleting="GridView1_RowDeleting"
OnSorting="GridView1_Sorting" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"
AllowPaging="True" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" AllowSorting="True" DataKeyNames="TeamNo">
<Columns>
<asp:BoundField DataField="TeamNo" HeaderText="团队编号" SortExpression="TeamNo"/>
<asp:BoundField DataField="TeamName" HeaderText="团队名称" SortExpression="TeamName"/>
<asp:BoundField DataField="TeamStrength" HeaderText="团队人数" SortExpression="TeamStrength"/>
<asp:BoundField DataField="TeamLeader" HeaderText="团队负责人" SortExpression="TeamLeader"/>
<asp:BoundField DataField="TeamPoints" HeaderText="团队积分" SortExpression="TeamPoints"/>
<asp:BoundField DataField="TeamAverage" HeaderText="团队平均积分" SortExpression="TeamAverage"/>
<asp:TemplateField HeaderText="编辑" >
<ItemTemplate>
<asp:LinkButton ID="btnEditOrder" runat="server" Text="编辑" CommandName="EditOrder" CommandArgument='<%# Eval("TeamNo") %>' OnCommand="SetMarried"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
后台主要代码
public int teamno;
//LinkButton的OnCommand事件的处理方法
public void SetMarried(Object sender, CommandEventArgs e)
{
teamno = Convert.ToInt32(e.CommandArgument.ToString());//强制转换
}//这里是根据上面的写的,取不到teamno,因此修改不成功
protected void Button7_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update Teams set TeamNo=@TeamNo,TeamName=@TeamName,TeamStrength=@TeamStrength,TeamLeader=@TeamLeader,TeamPoints=@TeamPoints,TeamAverage=@TeamAverage where TeamNo="+ teamno , con);// 我要取的就是这个teamno,这里是关键 cmd.Parameters.Add("@TeamNo", SqlDbType.Int);
cmd.Parameters.Add("@TeamName", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamStrength", SqlDbType.Int);
cmd.Parameters.Add("@TeamLeader", SqlDbType.NVarChar);
cmd.Parameters.Add("@TeamPoints", SqlDbType.Int);
cmd.Parameters.Add("@TeamAverage", SqlDbType.Decimal);
//SqlDataAdapter myda=new SqlDataAdapter();
//myda.UpdateCommand=cmd;
//myda.Update()
String sql1 = "select TeamNo from Teams where TeamNo=" + TextBox11.Text.Trim();
String sql2 = "select TeamName from Teams where TeamNo=" + TextBox12.Text.Trim();
SqlCommand cmd1 = new SqlCommand(sql1, con);
SqlCommand cmd2 = new SqlCommand(sql2, con);
SqlDataReader sdr1 = cmd1.ExecuteReader();
sdr1.Read();
if (sdr1.HasRows)
{
Response.Write("<script>alert('已存在该团队编号,请您重新输入!')</script>");
sdr1.Close();
con.Close();
}
else
{
sdr1.Close();
SqlDataReader sdr2 = cmd2.ExecuteReader();
sdr2.Read();
if (sdr2.HasRows)
{
Response.Write("<script>alert('已存在该团队名称,请您重新输入!')</script>");
sdr2.Close();
con.Close();
}
else
{
cmd.Parameters["@TeamNo"].Value = TextBox11.Text.Trim();
cmd.Parameters["@TeamName"].Value = TextBox12.Text.Trim();
cmd.Parameters["@TeamStrength"].Value = TextBox13.Text.Trim();
cmd.Parameters["@TeamLeader"].Value = TextBox14.Text.Trim();
cmd.Parameters["@TeamPoints"].Value = TextBox15.Text.Trim();
cmd.Parameters["@TeamAverage"].Value = TextBox16.Text.Trim();
sdr2.Close();
cmd.ExecuteNonQuery();
con.Close();
init();
div2.Visible = false;
}
}
bind();
}
这里是编辑按钮的id获得方法,就是这个id的值怎么能取到呢,这里的id跟teamno息息相关的。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
con.Open();
int id = Convert.ToInt32(e.CommandArgument.ToString());
#23
#24
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditOrder")
{
con.Open();
Button7.CommandArgument=e.CommandArgument;//这里把linkbutton的参数赋值给button7
.....
}
protected void Button7_Click(object sender, EventArgs e)
{
string id = (sender as Button).CommandArgument.ToString();
//或者string id = button7.CommandArgument.ToString();
}
上面的方式是在GridView1_RowCommand把CommandArgument传递给按钮,这样button7事件中也可以获取到该CommandArgument了。
或者也可以使用另外一种方式:
在函数之外定义一个变量,在GridView1_RowCommand中把CommandArgument的值给变量。在button7事件中取该变量的值。
{
if (e.CommandName == "EditOrder")
{
con.Open();
Button7.CommandArgument=e.CommandArgument;//这里把linkbutton的参数赋值给button7
.....
}
protected void Button7_Click(object sender, EventArgs e)
{
string id = (sender as Button).CommandArgument.ToString();
//或者string id = button7.CommandArgument.ToString();
}
上面的方式是在GridView1_RowCommand把CommandArgument传递给按钮,这样button7事件中也可以获取到该CommandArgument了。
或者也可以使用另外一种方式:
在函数之外定义一个变量,在GridView1_RowCommand中把CommandArgument的值给变量。在button7事件中取该变量的值。
#25
错误 2 无法将类型“object”隐式转换为“string”。存在一个显式转换(是否缺少强制转换?) d:\我的文档\Visual Studio 2005\WebSites\WebSite1\TeamManage.aspx.cs 305 39 d:\...\WebSite1\
//这个是在rowcommand中提示的(Button7.CommandArgument=e.CommandArgument;)初学半个月,你说方法有些能理解,但是我不会实现。
#26
#27
GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被选中的索引值
在linkbutton控件的Command事件,利用sender的Parent获取GridView中的当前行。
protected void lbtnQianChu_Command(object sender, CommandEventArgs e)
{
LinkButton lb = (LinkButton)sender;
DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
GridViewRow gvr = (GridViewRow)dcf.Parent; //此得出的值是表示那行被选中的索引值
}
LinkButton获取当前行或id办法http://ch-kexin.iteye.com/blog/566942
GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;//此得出的值是表示那行被选中的索引值
在linkbutton控件的Command事件,利用sender的Parent获取GridView中的当前行。
protected void lbtnQianChu_Command(object sender, CommandEventArgs e)
{
LinkButton lb = (LinkButton)sender;
DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
GridViewRow gvr = (GridViewRow)dcf.Parent; //此得出的值是表示那行被选中的索引值
}
LinkButton获取当前行或id办法http://ch-kexin.iteye.com/blog/566942