[已解决]如何将boundfiled值访问到变量中? [英] [Solved] How to access the boundfiled value into the variable ?

查看:85
本文介绍了[已解决]如何将boundfiled值访问到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想接受

Girdview的Id字段的值,如下所示

< asp:BoundField DataField =idHeaderText =ID可见=假/>



如何指定此值将参数传递给sqlquery?

I Wanted to accept the value of Id field of the
Girdview which is as follows
<asp:BoundField DataField ="id" HeaderText="ID" Visible="false" />

How can assign this value to pass the parameter to sqlquery ?

推荐答案

尝试类似



如果ID字段是PrimaryKey,请将其设置为Gridview的 DataKeyNames 属性。



然后在 RowUpdating 活动中你可以尝试访问



Try something like

If the ID field is the PrimaryKey , set it to the DataKeyNames property of the Gridview.

Then in the RowUpdating event you can try to access like

string ID= GridView1.DataKeys[e.RowIndex].Values["ID"].ToString();







SqlConnection con = new SqlConnection("YourConnectionString");

string query="UPDATE TableName set fieldName=@Parameter where Id=@ID";//parameter @ID,Can use StoredProcedure here instead of Inline Sql Query
con.Open();
SqlCommand Cmd = new SqlCommand(query, con);
Cmd.Parameters.AddWithValue("@ID", ID);
Cmd.ExecuteNonQuery();
GridView1.DataBind();


而不是绑定字段使用itemtemplate并将此ID绑定到链接按钮并重定向到另一个页面。这对你来说有点容易。





< asp:linkbutton id =lnkEditrunat =serverpostbackurl =yourpagename.aspx?ID ='< ;%#xmlns:asp =#unknown>'

CausesValidation =false文字=编辑>





i没有对此进行测试,但它应该可行。如果您有任何问题,请告诉我。
instead of bound filed use itemtemplate and bind this ID to link button and redirect to another page. this will be little bit easy for you.


<asp:linkbutton id="lnkEdit" runat="server" postbackurl="yourpagename.aspx?ID='<%#" xmlns:asp="#unknown">'
CausesValidation="false" Text="Edit">


i did not test this but it should work. let me know if you have any issues.


这篇关于[已解决]如何将boundfiled值访问到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆