通过获得来自按键ASP的BoundField和存储到SQL数据库的价值点击? [英] get value from asp boundfield and store to sql database via button click?
本文介绍了通过获得来自按键ASP的BoundField和存储到SQL数据库的价值点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从表单1(管理方)按钮被点击时,这个值:
i want to get this value from webform 1 (admin side) when button is clicked:
下面是我的全部viewcreditrequest code:
here is my full viewcreditrequest code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover table-striped"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress"
SortExpression="EmailAddress" />
<asp:BoundField DataField="CompanyAddress" HeaderText="CompanyAddress"
SortExpression="CompanyAddress" />
<asp:BoundField DataField="IncomeRange" HeaderText="IncomeRange"
SortExpression="IncomeRange" />
<asp:BoundField DataField="CreditRequest" HeaderText="CreditRequest"
SortExpression="CreditRequest" />
<asp:BoundField DataField="ContactNumber" HeaderText="ContactNumber" SortExpression="ContactNumber" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%# Eval("CreditRequest") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
背后code:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["IslandGasAdminFM"] != null)
{
bindgrid();
Label1.Text = "- Finance Manager";
}
else
{
Response.Write("<script>alert('Finance Manager credentials needed'); window.location.href='LogIn.aspx';</script>");
}
}
public void bindgrid()
{
SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
SqlCommand cmd = new SqlCommand("select * from CreditRequests ", conn);
SqlDataAdapter da = new SqlDataAdapter("", conn);
da.SelectCommand = new SqlCommand("select * from CreditRequests", conn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
和传递给Web窗体2(客户端):
and pass it on to webform 2 (client side):
<asp:TextBox ID="txtCredit" runat="server"></asp:TextBox>
这是我走到这一步:
管理方:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnApprove" runat="server" Text="Approve" OnClick ="btnApprove_Click" />
</ItemTemplate>
</asp:TemplateField>
背后code:
protected void btnApprove_Click(object sender, EventArgs e)
{
//code for getting the boundfield "creditrequest" and if possible, store it in database for future use.
}
有没有办法摆脱的BoundField的creditrequest值,并将其存储在数据库中?
is there any way to get the creditrequest value from boundfield and store it in database?
推荐答案
复制下面正是因为我展示code,它会工作。我刚刚在我的电脑上测试:
Copy the code below EXACTLY as I'm showing and it will work. I've just tested on my computer:
.ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="CreditRequest" HeaderText="Credit Request" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%# Eval("CreditRequest") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后面的 code:
public partial class delete_me : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//THIS IS IMPORTANT.GridView1_RowCommand will not fire unless you add this line
{
var p1 = new Person() { Name = "Person 1",CreditRequest="Credit Request 1" };
var p2 = new Person() { Name = "Person 2",CreditRequest="Credit Request 2" };
var list = new List<Person> { p1, p2 };
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Diagnostics.Debugger.Break();
if(e.CommandName == "Approve")
{
string creditRequest = e.CommandArgument as string;
}
}
}
public class Person
{
public string Name { get; set; }
public string CreditRequest { get; set; }
}
这篇关于通过获得来自按键ASP的BoundField和存储到SQL数据库的价值点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文