0

ページに 2 つのグリッドビューがあります。GridView1 には、次のような選択リンク、ID、および名前を含む列があります。

Select | Id | Name
select | 101 | Jack
select | 102 | Cath

今、私がやろうとしているのは、ジャックである最初の行から選択をクリックしたとしましょう。今、私の GridView2 はジャックによって注文された製品を次のように表示します:

Id | ProductID
101 | 111
101 | 222
101 | 333

そして、私が選択した Cath の場合、GridView2 は Cath によって注文された表示製品を変更します。

Id | productID
102 | 111
102 | 333
102 | 555

つまり、GridView1 から選択した行に基づいて GridView2 にデータを入力しようとしています。C# で asp.net を使用しています。

4

4 に答える 4

0

私の意見では、これを行う最善の方法はUpdatePanel、2 番目のグリッドをバインドするために使用することです。これを使用すると、ポストバックがないという利点があります (実際にはポストバックですが、ユーザーはそれに気づきません..)...

そして、を使用しない場合UpdatePanel、ポストバックでバインドした後にデータを表示する方法はありません (Javascript を介して行う場合を除きますが、これは面倒です)...これを実現するためのサンプル コードは次のとおりです。 :

ASPX ページ:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="GridView2" runat="server" >
            <Columns>

            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel runat="server">
    <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:LinkButton ID="asd" Text='Select' runat="server" OnClick="link_click"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

コードビハインド:

protected override void PageLoad(object sender, System.EventArgs e)
{

    if (!IsPostBack)
    {

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

    }
}
private DataTable getDataSource()
{

    SqlConnection conn = new SqlConnection("Server=192.168.1.1;DATABASE=dummy;UID=****;PWD=*****;");    //Example connString
    SqlCommand comm = new SqlCommand("SELECT * FROM Table1", conn);
    SqlDataAdapter ad = new SqlDataAdapter(comm);

    DataTable ta = new DataTable();
    ad.Fill(ta);

    return ta;
}

protected void button_click(object sender, EventArgs e)
{
    LinkButton asd = (LinkButton)sender;
    GridViewRow row = (GridViewRow)asd.NamingContainer;     //Gets the selected Row

    string user_id = row.Cells[2].Text;     //Use this to get any value you want.
    //Can now use the user_name to get the data for the grid 2, and update the panel
    GridView2.DataSource = getDataSource2(user_id);
    GridView2.DataBind();
    UpdatePanel1.Update();
}
private DataTable getDataSource2(string user_id)
{
    string sql = "SELECT * FROM TABLE2 WHERE user_id = @user_id";
    SqlConnection conn = new SqlConnection("Server=sqlserver\\sql2008;DATABASE=esm80;UID=sa;PWD=sa;");    //Example connString
    SqlCommand comm = new SqlCommand();
    comm.CommandText = sql;
    comm.Parameters.AddWithValue("@name", user_id);
    comm.Connection = conn;

    SqlDataAdapter ad = new SqlDataAdapter(comm);

    DataTable ta = new DataTable();
    ad.Fill(ta);

    return ta;
}

ここで説明するとUpdatePanel、GridView2 は、データがバインドされると gridView2 を更新するためにあります (これにより、新しくバインドされたデータが表示されます)。GridView1のUpdatePanelは、リンク ボタンからのポストバックを防ぐためにあります。

これがあなたの質問に答えることを願っています。

于 2013-06-07T13:08:03.843 に答える
0

これは私のサンプルコードです......

  protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<Data> DataList = new List<Data>() { new Data { id = 1, id2 = 2 }, new Data { id = 3, id2 = 4 } };
        GridView1.DataSource = DataList;
        GridView1.DataBind();
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow row = (GridViewRow ) btn.NamingContainer;

    Label slNoLabel = (Label) row.FindControl("slNoLabel");
    // function to get data based on label vlue
    GridView2.DataSource=GetData(slNoLabel.Text);
    GridView2.DataBind();

}

DataTable GetData(string value)
{
DataTable tbl = new DataTable ();
    //   Calling DB 
    return tbl;
    }
}

public class Data
{
    public int id { get; set; }
    public int id2 { get; set; }
}

そしてUIで

 <div>
    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Select" OnClick="Button1_Click" Text="Select" />
                    <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:GridView ID="GridView2" runat="server"></asp:GridView>
</div>
于 2013-06-07T11:39:05.660 に答える