从asp boundfield获取值并通过按钮点击存储到sql数据库?

时间:2016-05-28 09:01:22

标签: asp.net textbox boundfield

我希望在点击按钮时从webform 1(管理员端)获取此值:

这是我的完整viewcreditrequest代码:

 <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>
代码背后的代码:

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();
    }

并将其传递给webform 2(客户端):

<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>
代码背后的代码:

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值并将其存储在数据库中?

2 个答案:

答案 0 :(得分:0)

在这里你可以做两件事1.since你正在使用Button,在gridview中添加一个像这样的新行

<asp:TemplateField HeaderText="Actions">
    <ItemTemplate>
        <asp:Button CommandArgument='<%#Eval("CreditRequest") %>' runat="server" Text="View" ID="btnEdit" OnClick="btnEdit_OnClick"/>
      </ItemTemplate>
  </asp:TemplateField>

后面的代码就像这样

 protected void btnEdit_OnClick(object sender, EventArgs e)
        {
            if (sender != null)
            {
                string id = ((Button)sender).CommandArgument.ToString();
                Response.Redirect("yourpagename.aspx?value=" + id);
            }
        } 

在另一个页面上,客户端获取类似

的查询字符串值
string value = Request.QueryString["value"])

第二和简单的方法将是使用超链接而不是像这样的按钮

 <asp:TemplateField HeaderText="Actions">
     <ItemTemplate>
        <asp:HyperLink runat="server" NavigateUrl='<%# Eval("CreditRequest", "~/yourpagename.aspx?value={0}") %>' Text="Edit" "></asp:HyperLink>
      </ItemTemplate>
   </asp:TemplateField>

答案 1 :(得分:0)

在我显示的时候,将以下代码完全复制,它会起作用。我刚刚在我的电脑上测试过:

<强> .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>

代码背后:

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; }
}