简体   繁体   English

GridView 和显示面板:C# 和 ASP.NET

[英]GridView and Showing a Panel : C# and ASP.NET

I'm new to C# and asp.net and I have a GridView that has enabled selection on it and I put a button that is called View Products.我是 C# 和 asp.net 的新手,我有一个 GridView 启用了选择功能,我放置了一个名为“查看产品”的按钮。 When a user clicks on View Products, I want to show a panel that has more information for the button.当用户单击“查看产品”时,我想显示一个面板,其中包含有关该按钮的更多信息。

I have tried everything and looked up multiple ways but nothing is helping me find an answer to my problem.我已经尝试了所有方法并查找了多种方法,但没有任何方法可以帮助我找到问题的答案。

Picture of the GridView图片GridView

Ok, this is common, but answers are varied, and I tend to not like how most answers work.好的,这很常见,但答案多种多样,而且我倾向于不喜欢大多数答案的工作方式。

I suggest this approach:我建议这种方法:

On that page, drop in a "div" with the controls and even additional buttons (such as perhaps a delete command, save command, and "cancel" to simple go back to the grid view.在该页面上,放入带有控件甚至附加按钮(例如删除命令、保存命令和“取消”到简单的 go 回到网格视图)的“div”。

So, first up, our grid view say like this:所以,首先,我们的网格视图是这样的:

        <asp:GridView ID="GridView1" runat="server"
            AutoGenerateColumns="False" DataKeyNames="ID"
            CssClass="table" Width="45%" ShowHeaderWhenEmpty="true">
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
                <asp:BoundField DataField="City" HeaderText="City" />
                <asp:BoundField DataField="Description" HeaderText="Description" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="cmdEdit" runat="server" Text="Edit"
                            CssClass="btn myshadow"
                            OnClick="cmdEdit_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

And say right below the above, we can simple drop in some standard text boxes and controls inside of a "div" that we hide/show for the details when clicked on.在上面的正下方说,我们可以简单地将一些标准文本框和控件放入我们在单击时隐藏/显示详细信息的“div”内。

So, say like this:所以,像这样说:

  <div id="EditRecord" runat="server"
      style="float:left;display:none;border:solid 2px;padding:15px">
        <div style="float:left" class="iForm">
                <label>HotelName</label>
                <asp:TextBox ID="txtHotel" runat="server" width="280" f="HotelName"/>  <br />
                <label>First Name</label>
                <asp:TextBox ID="tFN" runat="server"  Width="140" f="FirstName" /> <br />
                <label>Last Name</label>
                <asp:TextBox ID="tLN" runat="server"  Width="140" f="LastName"  /> <br />
                <label>City</label>
                <asp:TextBox ID="tCity" runat="server"  Width="140" f="City"  />
                <br />
                <label>Province</label>
            <asp:TextBox ID="tProvince" runat="server" Width="75" f="Province"   ></asp:TextBox> <br />
        </div>
        <div style="float:left;margin-left:20px" class="iForm">
            <label>Description</label> <br />
            <asp:TextBox ID="txtNotes" runat="server" Width="400" TextMode="MultiLine" 
                Height="150px" f="Description" ></asp:TextBox> <br />
            <asp:CheckBox ID="chkActive" Text=" Active" runat="server" TextAlign="Right" />
            <asp:CheckBox ID="chkBalcony"  Text=" Has Balcony" runat="server" TextAlign="Right" />
        </div>
        <div style="clear:both"></div>

        <button id="cmdSave" runat="server" class="btn myshadow" onserverclick="cmdSave_ServerClick" >
            <span aria-hidden="true" class="glyphicon glyphicon-floppy-saved"> Save</span> 
        </button>

        <button id="cmdCancel" runat="server" class="btn myshadow" 
            style="margin-left:15px" onserverclick="cmdCancel_ServerClick"  >
            <span aria-hidden="true" class="glyphicon glyphicon-arrow-left"> Back/Cancel</span>
        </button>

        <button id="cmdDelete" runat="server" class="btn myshadow" style="margin-left:15px">
            <span aria-hidden="true" class="glyphicon glyphicon-trash"> Delete</span>
        </button>  
      </div>

Again, nothing speical - just controls and text boxes.同样,没有什么特别的——只有控件和文本框。

So, now our code to load can be this:所以,现在我们要加载的代码可以是这样的:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid();
    }

    void LoadGrid()
    {
        
        GridView1.DataSource = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName");
        GridView1.DataBind();
    }

Code for when we click on a button:当我们点击按钮时的代码:

Now we can't (like normal) double click on the button to create the event since that button is "nested" in the gv.现在我们不能(像往常一样)双击按钮来创建事件,因为该按钮“嵌套”在 gv 中。 So, in the markup, just type in for the button所以,在标记中,只需输入按钮

onclick=点击=

在此处输入图像描述

When you hit "=" sign, a popup gives you the option to create a plane jane click event for the button.当您点击“=”符号时,弹出窗口会为您提供为按钮创建 plane jane 单击事件的选项。 So, choose create event.因此,选择创建事件。

Thus the button in the gv becomes this:因此 gv 中的按钮变成了这样:

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="cmdEdit" runat="server" Text="Edit"
                            CssClass="btn myshadow"
                            OnClick="cmdEdit_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>

And now our button click event:现在我们的按钮点击事件:

it will get the row, load up the controls in that div, and then display the div.它将获取该行,加载该 div 中的控件,然后显示该 div。

I have this code:我有这段代码:

    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        Button cmdEdit = (Button)sender;
        GridViewRow gRow = (GridViewRow)cmdEdit.NamingContainer;
        int PKID = (int)GridView1.DataKeys[gRow.RowIndex]["ID"];
        DataTable rstData = MyRst($"SELECT * FROM tblHotelsA WHERE ID = {PKID}");

        General.FLoader(EditRecord, rstData.Rows[0]); // load up the controls

        GridView1.Style.Add("display","none");
        EditRecord.Style.Add("display", "normal");

    }

And the results are thus this:结果是这样的:

在此处输入图像描述

Note that the cancel button is rather simple.请注意,取消按钮非常简单。

You hide the "div" and re-show (un-hide) the grid.您隐藏“div”并重新显示(取消隐藏)网格。

this:这个:

    protected void cmdCancel_ServerClick(object sender, EventArgs e)
    {
        GridView1.Style.Add("display", "normal");
        EditRecord.Style.Add("display", "none");

    }

And I did use a helper routine (just returns a table).我确实使用了一个帮助程序(只返回一个表)。

That was this:那是这样的:

    DataTable MyRst(string strSQL)
    {
        DataTable rstData = new DataTable();
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                cmdSQL.Connection.Open();
                rstData.Load(cmdSQL.ExecuteReader());
            }
        }
        return rstData;
    }

So above is the basic idea, and workings.以上是基本思想和工作原理。

And we could now that we have a div?现在我们可以有一个 div 了吗?

We could call a jQuery.UI "dialog", and that would in place of the div hide/show, simple call jQuery.UI, and then you get this effect:我们可以将 jQuery.UI 称为“对话框”,这将代替 div 隐藏/显示,只需调用 jQuery.UI,然后您将获得以下效果:

在此处输入图像描述

I can post the code for how that pop up works, but really, the simple idea is a simple button, get PK row id, load that data into some controls, then hide the grid, show the div, and you off and running.我可以发布弹出窗口如何工作的代码,但实际上,简单的想法是一个简单的按钮,获取 PK 行 ID,将该数据加载到一些控件中,然后隐藏网格,显示 div,然后你就可以运行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM