5

以前のコードについて申し訳ありません。誰か助けてもらえますか?3列のExcelシートからインポートすることGridView1で入力されるグリッドビューがあります。PageLoad()

  1. 日にち
  2. お客様
  3. PayingBookNoOrDD

シートには5行あります。ユーザーは、ページのテキストボックス(下のマークアップ)のデータを編集できます。編集後、ユーザーが送信ボタンをクリックすると、すべてのテキストボックスからこれらの新しい値を取得し、入力された場所から同じExcelシートを更新する必要がありますGridView

3つの文字列配列を作成しました:、、dateArrayおよびcustArrayこれらpayingInBookArrayの新しい値を格納します。しかし、アプリケーションを実行すると、3つの配列はすべて空になります。

マークアップ:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Date,Customers,PayingInBookNoOrDD" >
    <Columns>
    <asp:TemplateField>
        <HeaderTemplate>Date</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtDate" Text='<%# Bind("Date") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <HeaderTemplate>Customers</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtCustomers" Text='<%# Bind("Customers") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <HeaderTemplate>PayingInBookNoOrDD</HeaderTemplate>
        <ItemTemplate>
            <asp:TextBox runat="server" ID="txtPayingInBookNoOrDD" Text='<%# Bind("PayingInBookNoOrDD") %>'></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:Button ID="txtSubmit" runat="server" Text="Submit" onclick="txtSubmit_Click" />

コードビハインド:

protected void Page_Load(object sender, EventArgs e)
{
    string selectQuery = "SELECT * FROM [Month1$B2:D5]";
    OleDbConnection conn = new OleDbConnection(connString);

    conn.Open();

    OleDbDataAdapter da = new OleDbDataAdapter(selectQuery, conn);
    DataSet ds = new DataSet();
    da.Fill(ds);

    GridView1.DataSource = ds;
    GridView1.DataBind();

    conn.Close();
    da.Dispose();
    conn.Dispose();
}

protected void txtSubmit_Click(object sender, EventArgs e)
{
    IList<string> DateArray = new List<string>();
    IList<string> custArray = new List<string>();
    IList<string> payInBookArray = new List<string>();

    foreach (GridViewRow gr in GridView1.Rows)
    {
        TextBox lblDate = (TextBox)gr.Cells[0].FindControl("txtDate");
        DateArray.Add(lblDate.Text);

        TextBox lblCustomers = (TextBox)gr.Cells[1].FindControl("txtCustomers");
        custArray.Add(lblCustomers.Text);

        TextBox lblPayInBookNo = (TextBox)gr.Cells[2].FindControl("txtPayingInBookNoOrDD");
        payInBookArray.Add(lblPayInBookNo.Text);
    }

    ExportToExcel(DateArray.ToArray(), custArray.ToArray(), payInBookArray.ToArray()); 

}

誰かが解決策を持っているかどうか私に知らせてください。

ありがとう。

4

3 に答える 3

0

個人的に、私はあなたのtxtSubmit_Click機能をこれに変更します:

protected void txtSubmit_Click(object sender, EventArgs e)
{
    IList<string> DateArray = new List<string>();
    IList<string> custArray = new List<string>();
    IList<string> payInBookArray = new List<string>();

    foreach (GridViewRow gr in GridView1.Rows)
    {
        TextBox lblDate = (TextBox)gr.FindControl("txtDate");
        DateArray.Add(lblDate.Text);

        TextBox lblCustomers = (TextBox)gr.FindControl("txtCustomers");
        custArray.Add(lblCustomers.Text);

        TextBox lblPayInBookNo = (TextBox)gr.FindControl("txtPayingInBookNoOrDD");
        payInBookArray.Add(lblPayInBookNo.Text);
    }

    ExportToExcel(DateArray.ToArray(), custArray.ToArray(), payInBookArray.ToArray()); 

}

.Cellsコレクション内の値に直接アクセスしようとすると、常に問題が発生します。.FindControl行自体を呼び出すとどうなりますか?

他の人が言っているように、HTMLフィールドと変数の新しい名前について考える価値があります。DateArrayタイプを持っていることは今では些細なことのように思えますIListが、それは私にループを見せるために簡単に投げましたDateArray.ToArray()。命名規則やその他の小さなソースコードの変更は、今すぐ修正するのにそれほど時間はかかりませんが、他のプロジェクトで数週間または数か月作業した後でこのコードを再検討する必要がある場合は、後で多くの時間を節約できます。

于 2011-10-07T15:05:05.733 に答える
0
Protected Sub txtNombres_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("FiltroNombres") = DirectCast(sender, TextBox).Text

End Sub
于 2014-04-25T21:25:53.300 に答える
0

Page_Load イベントにポストバック チェックを追加します。あなたの btn_Submit コードに問題はありません。

protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack){
         string selectQuery = "SELECT * FROM [Month1$B2:D5]";
         OleDbConnection conn = new OleDbConnection(connString);
         conn.Open();
         OleDbDataAdapter da = new OleDbDataAdapter(selectQuery, conn);
         DataSet ds = new DataSet();
         da.Fill(ds);
         GridView1.DataSource = ds;
         GridView1.DataBind();
         conn.Close();
         da.Dispose();
         conn.Dispose();
    }
}
于 2011-10-07T05:34:43.853 に答える