在Gridview中添加动态行以及如何在Gridview中保留用户控件中的选定选项

时间:2014-12-19 11:45:15

标签: c# asp.net

我创建了名为CRE.ascx的用户控件,此控件有3个下拉列表。 第一个下拉列表在pageload.Second和third上基于SelectedIndexChanged绑定数据。

    <table cellspacing="0" cellspading="0" style="width:550px;height:30px;">
        <tr>
            <td style="width:30%;">
                <asp:DropDownList ID="ddlCRE" runat="server" Height="20px" Width="145px" OnSelectedIndexChanged="ddlCRE_SelectedIndexChanged" AutoPostBack="true">
                </asp:DropDownList>
            </td>
            <td style="width:30%;">
                <asp:DropDownList ID="ddlDataPoints" runat="server" Height="20px" Width="145px" OnSelectedIndexChanged="ddlDataPoints_SelectedIndexChanged" AutoPostBack="true">
                </asp:DropDownList>
            </td>
            <td style="width:30%;">
                <asp:DropDownList ID="ddlErrorCode" runat="server" Height="20px" Width="145px" OnSelectedIndexChanged="ddlErrorCode_SelectedIndexChanged" AutoPostBack="true">
                </asp:DropDownList>
            </td>
            <td style="width:10%;">
                <asp:TextBox ID="tbxErrorScore" runat="server" style="height:17px;border:0px;font-family:'Segoe UI';font-size:13px;font-weight:500;color:white;background-color:#333333;
                 width:65px;" ReadOnly="true"> </asp:TextBox>
            </td>
        </tr> 
</table>



 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Get Ticket Type Values
            dbtickettype = helpdsktcktusrHandler.GetTicketType("DPT0001");

            ddlCRE.DataSource = dbtickettype;
            ddlCRE.DataValueField = "Type_ID";
            ddlCRE.DataTextField = "Type_Name";
            ddlCRE.DataBind();
            ddlCRE.Items.Insert(0, new ListItem("Select Type", "SLCT0000"));
        }
        else
        {

        }
    }

    protected void ddlCRE_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedTypeID = ddlCRE.SelectedValue.ToString();
        try
        {
            if (selectedTypeID == "SLCT0000")
            {
                ddlDataPoints.Items.Clear();
                ddlErrorCode.Items.Clear();
                tbxErrorScore.Text = string.Empty;
            }
            else
            {
                //Get Category Details
                dbticketCategory = helpdsktcktusrHandler.GetTicketCategoryDetails(selectedTypeID);
                //Binding Ticket Type values to Listbox
                ddlDataPoints.DataSource = dbticketCategory;
                ddlDataPoints.DataValueField = "Category_ID";
                ddlDataPoints.DataTextField = "Category_Name";
                ddlDataPoints.DataBind();
                ddlDataPoints.Items.Insert(0, new ListItem("Select Category", "SLCT0000"));
                //Clear Items
                ddlErrorCode.Items.Clear();
                tbxErrorScore.Text = string.Empty;
            }
        }
        catch (Exception ex)
        {

        }
    }

    protected void ddlDataPoints_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedCatID = ddlDataPoints.SelectedValue.ToString();
        try
        {
            if (selectedCatID == "SLCT0000")
            {
                ddlErrorCode.Items.Clear();
                tbxErrorScore.Text = string.Empty;
            }
            else
            {
                //Get Category Details
                dbticketSubCategory = helpdsktcktusrHandler.GetTicketSubCategoryDetails(selectedCatID);
                //Binding Ticket Type values to Listbox
                ddlErrorCode.DataSource = dbticketSubCategory;
                ddlErrorCode.DataValueField = "Sub_Category_ID";
                ddlErrorCode.DataTextField = "Sub_Category_Name";
                ddlErrorCode.DataBind();
                ddlErrorCode.Items.Insert(0, new ListItem("Select Subcategory", "SLCT0000"));
                //Clear Items
                tbxErrorScore.Text = string.Empty;
            }
        }
        catch (Exception ex)
        {
        }
    }

    protected void ddlErrorCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedSubcatID = ddlErrorCode.SelectedValue.ToString();
        try
        {
            if (selectedSubcatID == "SLCT0000")
            {
                tbxErrorScore.Text = string.Empty;
            }
            else
            {
                //Get Category Details
                dbticketIssues = helpdsktcktusrHandler.GetTicketIssueDetails(selectedSubcatID);
                ////Binding Ticket Type values to Listbox
                //ddlstIssue.DataSource = dbticketIssues;
                //ddlstIssue.DataValueField = "IssueID";
                //ddlstIssue.DataTextField = "Issue_Name";
                //ddlstIssue.DataBind();

                tbxErrorScore.Text = dbticketIssues.Rows[0][1].ToString();

            }
        }
        catch (Exception ex)
        {

        }
    }

然后注册指令和添加到页面的用户控件的实例。 在这个主页面中,我添加了一个Gridview,用户控件UC1和两个按钮 包含在ItemTemplate中。

<%@ Register src="~/CRE.ascx" TagName="InsertNewCRE" TagPrefix="uc1"  %>
<asp:UpdatePanel ID="MainUpdatePanel" runat="server">
    <ContentTemplate>
        <div id="dvsubCRE" class="dvsubCRE" runat="server">
            <!-----[[[ GRIDVIEW ADDING CRE  ]]]----->
            <div id="dvAddingErrorInfo" class="dvAddingErrorInfo">
                <!-- LOAD CRE DROPDOWN INFO GRID  -->
                <asp:GridView ID="gvCREInfo" runat="server" CssClass="gvErrorInfo" AlternatingRowStyle-CssClass="" ShowFooter="false" ShowHeader="false"
                EnableViewState="True" GridLines="None" EmptyDataText="No records found" AutoGenerateColumns="true" CaptionAlign="Left" CellPadding="0"
                ShowHeaderWhenEmpty="True" OnRowCreated="gvCREInfo_RowCreated" OnRowCommand="gvCREInfo_RowCommand" >
                    <Columns>
                        <asp:BoundField DataField="RowNumber" />
                        <asp:TemplateField ItemStyle-Width="25%" ItemStyle-Height="20px">
                            <ItemTemplate>
                                <uc1:InsertNewCRE id="UC1InserCRE" runat="server" EnableViewState="true" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-Width="5%">
                            <ItemTemplate>
                                <asp:ImageButton ID="imgbtnAddCRE" runat="server"  Width="20px" Height="20px"  value=""  ImageUrl="~/Images/Tracker/add.png"
                                CommandName="ButtonAddCRERow" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"  />
                                <asp:ImageButton ID="imgbtnReoveCRE" runat="server" Width="20px" Height="20px"  value="" Visible="false"  ImageUrl="~/Images/Tracker/delete.png" CommandName="ButtonRemoveCRERow" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <!-- LOAD CRE DROPDOWN INFO GRID CLOSE -->
            </div>
            <!-----[[[ GRIDVIEW ADDING CRE CLOSE ]]]----->
        </div>            

    </ContentTemplate>
</asp:UpdatePanel>

当主页加载时,用户控制UC1加载到gridview中并从CRE.ascx页面中提取数据。我将页面加载时的虚拟数据绑定到gridview.Add新行以及RowCommand中提到的用户控件。

protected void Page_Load(object sender, EventArgs e)
        {
                try
                {
                    if (!IsPostBack)
                    {
                        DataTable dt = new DataTable();
                        DataRow dr = null;
                        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
                        dr = dt.NewRow();
                        dr["RowNumber"] = 1;
                        dt.Rows.Add(dr);

                        ViewState["CurrentTable"] = dt;

                        gvCREInfo.DataSource = dt;
                        gvCREInfo.DataBind();
                    }
                    else
                    {


                    }
                }
                catch (Exception ex)
                {

                }
        }

        protected void gvCREInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            #region ADD NEW CRE ROW
            if (e.CommandName == "ButtonAddCRERow")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvCREInfo.Rows[index];

                int count = gvCREInfo.Rows.Count;
                DataRow drCurrentRow = null;

                UserControl UC1 = (UserControl)(row.Cells[0].FindControl("UC1InserCRE"));
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];

                for (int i = 0; i <= (count - 1); i++)
                {
                    drCurrentRow = dtCurrentTable.NewRow();
                    dtCurrentTable.Rows.Add(drCurrentRow);
                }

                gvCREInfo.DataSource = dtCurrentTable;
                gvCREInfo.DataBind();
            }
            #endregion
        }

当我运行此代码正常工作时,我更改了第一个下拉列表 它会拉出数据并绑定到第二个,就像明智的第三个一样。但是当我点击添加按钮时,选择的数据在第一行和第二行控件中丢失而不是数据。如何保留现有的选定数据并加载用户控制有了数据,它不应该丢失数据事件。请帮助我解决这个问题。 ![在此输入图像说明] [1]

http://i.stack.imgur.com/wdYZv.jpg

0 个答案:

没有答案