按下按钮时,如何理解哪些列值?

时间:2012-11-10 12:59:08

标签: c# asp.net gridview

我尝试在aspx中编写数据网格。在我的数据库中有一个名为'PROJECT_NAME'的列。我想在我的数据网格中使用这些列,然后添加第二列(editbox)以在每个项目中添加值。这都在网格视图中,我在表单中添加按钮。当我按下按钮时,我如何理解哪个列值属于哪个项目?

我的意思是,例如

  1. 行值:项目2列值:50
  2. 行值:B项目2列值:60
  3. 行值:C项目2列值:70
  4. 我想知道何时按下按钮1.项目值50怎么能理解这个?

    这是我的aspx代码:

         <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head runat="server">
            <title></title>
            <style type="text/css">
                .style1
                {
                    width: 78px;
                }
                .style2
                {
                width: 426px;
            }
        </style>
    </head>
    
    
    <body>
        <form id="form1" runat="server">
        <div style="height: 561px">
    
            <table style="width: 100%; height: 556px;">
                <tr>
                    <td>
                        &nbsp;</td>
                    <td>
    
                        <table style="width:100%;">
                            <tr>
                                <td class="style1">
                                    Ay</td>
                                <td>
                                    <asp:TextBox ID="ay" runat="server"></asp:TextBox>
                                </td>
                                <td>
                                    &nbsp;</td>
                            </tr>
                            <tr>
                                <td class="style1">
                                    Yıl</td>
                                <td>
                                    <asp:TextBox ID="yil" runat="server"></asp:TextBox>
                                </td>
                                <td>
                                    &nbsp;</td>
                            </tr>
                            <tr>
                                <td class="style1">
                                    &nbsp;</td>
                                <td>
                                    &nbsp;</td>
                                <td>
                                    &nbsp;</td>
                            </tr>
                        </table>
                    </td>
    
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td>
                        &nbsp;</td>
                    <td>
                       <asp:GridView ID="portföy" runat="server" 
                        CellPadding="3" GridLines="Horizontal" 
                        AutoGenerateColumns="False"      
                        DataKeyNames="PRJ_PROJECT_NAME" BackColor="White" BorderColor="#E7E7FF" 
                        BorderStyle="None" BorderWidth="1px" Font-Bold="False" Font-Names="Verdana" 
                        Font-Overline="False" Font-Size="Smaller" Width="1000px"  
                        >
                        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <Columns> 
                <asp:TemplateField HeaderText="PRJ_PROJECT_NAME"  HeaderStyle-HorizontalAlign="Left"> 
    
                    <ItemTemplate> 
                        <asp:Label ID="lblProjeTT" runat="server" Text='<%# Bind("PRJ_PROJECT_NAME") %>'></asp:Label> 
                    </ItemTemplate> 
                    <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                </asp:TemplateField> 
    
                <asp:TemplateField HeaderText="Miktar (a/g)" HeaderStyle-HorizontalAlign="Center"> 
    
                    <ItemTemplate> 
                   <asp:TextBox runat="server" id="txtField1" Text='<%# Bind("TAMAMLANMA_YUZDESI") %>'/>
                    </ItemTemplate>  
                    <EditItemTemplate>
                     <asp:TextBox runat="server" id="txtField1" Text='<%# Bind("TAMAMLANMA_YUZDESI") %>'/>
                    </EditItemTemplate>
    
                    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                </asp:TemplateField>             
                <asp:TemplateField HeaderText="" ShowHeader="False" HeaderStyle-HorizontalAlign="Center"> 
    
                    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                </asp:TemplateField> 
            </Columns> 
                        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
                        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <EditRowStyle BackColor="#FFFFA8" ForeColor="Black" />
                        <AlternatingRowStyle BackColor="#F7F7F7" />
                    </asp:GridView>
    
                        <table style="width:100%;">
                            <tr>
                                <td class="style2">
                                    &nbsp;</td>
                                <td>
                                    &nbsp;</td>
                                <td>
                                    &nbsp;</td>
                            </tr>
                            <tr>
                                <td class="style2">
                                    &nbsp;</td>
                                <td>
                                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 
                                        Width="149px" />
                                </td>
                                <td>
                                    &nbsp;</td>
                            </tr>
                            <tr>
                                <td class="style2">
                                    &nbsp;</td>
                                <td>
                                    &nbsp;</td>
                                <td>
                                    &nbsp;</td>
                            </tr>
                        </table>
    
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
                <tr>
                    <td>
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                    <td>
                        &nbsp;</td>
                </tr>
            </table>
    
        </div>
        </form>
    </body>
    </html>
    

    这是我的代码:

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.OleDb;
    using System.Data;
    using System.Configuration;
    
    public partial class _Default : System.Web.UI.Page
    {
        csOra cOra;
        string oraTns = ConfigurationManager.AppSettings["TNS"];
        string oraUserName = ConfigurationManager.AppSettings["OraUserName"];
        string oraPassword = ConfigurationManager.AppSettings["OraPassword"];
    
        protected void Page_Load(object sender, EventArgs e)
    
        {
    
              cOra = new csOra(oraTns, oraUserName, oraPassword);
            if (cOra.OraCnn.State != ConnectionState.Open)
            {
                MessageBox.Show("Oracle ile bağlantı kurulamadı...");
                return;
            }
            try
            {
                if (!IsPostBack)
                { 
    
                    setGrid();
                }
    
            }
            catch (OleDbException ex)
            {
                Response.Write(@"Oracle Error:" + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                Response.Write(@"Error:" + ex.Message);
                return;
            }
    
        }
    
        void setGrid()
        {
            string sSQL = @"SELECT PRJ_PROJECT_NAME,TAMAMLANMA_YUZDESI
                            FROM  PPIGANTT.KOKTEYL_TABLE_3
                            WHERE PRJ_PROJECT_NAME like '%d%' 
                            ORDER BY Request_id";
            try
            {
                OleDbDataAdapter oDa = new OleDbDataAdapter(sSQL, cOra.OraCnn);
                DataTable portföy_ = new DataTable("KOKTEYL_TABLE_3");
                oDa.Fill(portföy_);
    
                if (portföy_.Rows.Count > 0)
                {
                    portföy.DataSource = portföy_;
                    portföy.DataBind();
                }
                else
                {
                    portföy_.Rows.Add(portföy_.NewRow());
                    portföy.DataSource = portföy_;
                    portföy.DataBind();
    
                    int TotalColumns = portföy.Rows[0].Cells.Count;
                    portföy.Rows[0].Cells.Clear();
                    portföy.Rows[0].Cells.Add(new TableCell());
                    portföy.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                    portföy.Rows[0].Cells[0].Text = "No Record Found";
                }
            }
            catch (OleDbException ex)
            {
                Response.Write(@"Oracle Error:" + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                Response.Write(@"Error:" + ex.Message);
                return;
            }
        }
        void UpdatePortföy(string Portföy, int bütçe    )
        {
            string sSQL = @"BEGIN UPDATE PPIGANTT.KOKTEYL_TABLE_3
                               SET TAMAMLANMA_YUZDESI = " + bütçe + @"WHERE PRJ_PROJECT_NAME ='"
                                             + Portföy + @"' ; COMMIT; END;";
            string sErr = "";
            try
            {
                cOra.ExecNonQuery(sSQL, out sErr);
            }
            //setGrid();
            catch (OleDbException ex)
            {
                Response.Write(@"Oracle Error:" + ex.Message);
                return;
            }
            catch (Exception ex)
            {
                Response.Write(@"Error:" + ex.Message);
                return;
            }
        }
    
    
    
        protected void Button1_Click(object sender, EventArgs e)
        {
           /*  what can I write here to understand whic column value ?*/
        } 
    }
    

    感谢。

2 个答案:

答案 0 :(得分:1)

在Button1_Click内部,您需要遍历GridView中的所有行,并在每一行上执行FindControl以获取标签和文本框。从那里你可以确定PRJ_PROJECT_NAME和文本框中的值是什么。

以下是一个例子:

Default.aspx的

<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false">
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:Label ID="lblLabel" runat="server" Text='<%# Bind("Number") %>' />
      </ItemTemplate>          
    </asp:TemplateField>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:TextBox ID="txtData" runat="server" Text='<%# Bind("Value") %>' />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    // Create some fake data and bind it to the gridview
    var data = new List<TheData>();

    foreach(var num in Enumerable.Range(1, 4))
    {
      var newData = new TheData();

      newData.Number = num.ToString();
      newData.Value = num;

      data.Add(newData);
    }

    gvData.DataSource = data;
    gvData.DataBind();
  }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
  for (var index = 0; index < gvData.Rows.Count; ++index)
  {
    var row = gvData.Rows[index];
    var lblLabel = row.FindControl("lblLabel") as Label;
    var txtData = row.FindControl("txtData") as TextBox;

    //Here is where the values are grabbed, at this point you can do what you need to.
    var number = lblLabel.Text;
    var value = txtData.Text;
  }
}

答案 1 :(得分:0)

我之前使用的一个解决方案利用了按钮的CommandArgument属性。您可以设置CommandArgument ='&lt;%Eval(“ColumnValueYouWant”)%&gt;'并且每个按钮将获得其行中列的值。然后,在click事件中,您可以将对象发送器解析回Button并访问CommandArgument值。