簡體   English   中英

當我按下按鈕時,如何了解哪些列有價值?

[英]How can I understand which columns value when I press the button?

我嘗試用aspx編寫數據網格。 我的數據庫中有一列名為“ PROJECT_NAME”。 我希望這些列在我的數據網格中,然后添加第二列(編輯框)以為每個項目添加值。 這全部在網格視圖中,我在表單中添加按鈕。 當我按下按鈕時,如何理解哪些列值屬於哪個項目?

我的意思是,例如

  1. 行值:項目2列值:50
  2. 行值:B項目2列值:60
  3. 行值:C項目2列值:70

我想知道何時按下按鈕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 ?*/
    } 
}

謝謝。

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

我以前使用的一種解決方案是利用按鈕的CommandArgument屬性。 您可以設置CommandArgument ='<%Eval(“ ColumnValueYouWant”)%>',每個按鈕將獲取其行中列的值。 然后,在click事件中,您可以將對象發送者解析回Button中,並訪問CommandArgument值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM