1

我正在做一个小项目,并以编程方式在带有控件的网格视图中切换文本。

以下是代码的步骤:

private GridViewRow EnterEditMode(GridView GV, int RowIndex) 
    {
    GridViewRow GVR = GV.Rows[RowIndex];
    foreach (TableCell TC in GVR.Cells)
    {
        if (TC == GVR.Cells[2])
        {                        
    PlaceHolder PH = new PlaceHolder();
    string CellContent1 = TC.Text;
    DropDownList DDL = new DropDownList();
    foreach (object s in Enum.GetValues(typeof(Ticket_Status)))
    {
    DDL.Items.Add(s.ToString());
    }
    DDL.SelectedValue = CellContent1;
    DDL.ID = "I_Status_CB";
    DDL.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
    PH.ID = "PH_Status_CB";
    PH.ViewStateMode = System.Web.UI.ViewStateMode.Enabled;
    PH.Controls.Add(DDL);
    this.Page.Controls.Add(PH);
    TC.Controls.Add(PH);
        }
    } 
}

现在问题来了:

我创建一个控件并用创建的控件替换表格单元格中的文本。
问题是我找不到从创建的控件中获取值的方法!
我已经尝试了所有我知道的方法,我尝试对一行、整个表和单元格本身使用 FindControl() 正常和递归。

如果您能发现问题或对此问题有任何想法,请回复!

友好的问候,

-雷博。

编辑:

看来我没有给你足够的信息来解决这个问题。
我做的第一件事是将来自 SQL 数据库的信息绑定到网格视图中声明的 BoundFields。

  private void GetTable()
        {
            var Tg = Ticket_Table;
            if (sCon != null && sCon.State == ConnectionState.Open)
            {            
                string SQL_Command = "SELECT * FROM Ticket_Table_2013";
                SqlDataSource sDsc = new SqlDataSource(SQL_ConnectString, SQL_Command);
                SqlDataAdapter sDap = new SqlDataAdapter(SQL_Command,sCon);
                DataTable DT = new DataTable();
                sDap.Fill(DT);            

                //This is where i'm adding extra columns for additional commands.
                Tg.DataSource = DT;
                Tg.DataBind();
                sDap.Dispose();
                sDsc.Dispose();
            }
            else { CONNECT(SQL_ConnectString); GetTable(); }
        }

这是 ASP.Net 代码:

<asp:GridView 
    ID="Ticket_Table" 
    CssClass="TableStyle" 
    ViewStateMode="Enabled" 
    runat="server" 
    OnRowUpdating="Ticket_Table_OnRowUpdating"

    OnSelectedIndexChanged="Ticket_Table_OnSelectedIndexChanged"
    AutoGenerateColumns="false"
    AllowPaging="true"
    >
    <HeaderStyle CssClass="bgBlack02 black center"/>  
    <RowStyle CssClass="bgBlack01 center" />
    <SelectedRowStyle CssClass="bgBlack02" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <div class="CmdImgWrap" id="CommandBox" runat="server">
                    <asp:ImageButton ID="I_Remove" CssClass="CmdImg" ImageUrl="style/Icons/Trashcan.png" runat="server" ToolTip="Remove entire row." OnClick="I_Remove_OnClick"/>
                    <asp:ImageButton ID="I_Edit" CssClass="CmdImg" ImageUrl="style/Icons/EditFile.png" runat="server" ToolTip="Enable edit mode." OnClick="I_Edit_OnClick"/>
                    <asp:ImageButton ID="I_Accept" CssClass="CmdImg" ImageUrl="style/Icons/Accept.png" Visible="false" runat="server" ToolTip="Accept changes." OnClick="I_Accept_OnClick"/>
                    <asp:ImageButton ID="I_Cancel" CssClass="CmdImg" ImageUrl="style/Icons/Cancel.png" Visible="false" runat="server" ToolTip="Disgard changed." OnClick="I_Cancel_OnClick"/>
                    <asp:ImageButton ID="I_Highlight" CssClass="CmdImg" ImageUrl="style/Icons/HighLight.png" runat="server" ToolTip="Highlight entire row."  OnClick="I_Highlight_OnClick"/>
                </div>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Ticket_ID" HeaderText="Ticket ID" SortExpression="Ticket_ID" />
        <asp:BoundField DataField="Ticket_Status" HeaderText="Ticket status" SortExpression="Ticket_Status" />
        <asp:BoundField DataField="Ticket_Title" HeaderText="Ticket title" SortExpression="Ticket_Title" />
        <asp:BoundField DataField="Ticket_Description" HeaderText="Description" SortExpression="Ticket_Description" />
        <asp:BoundField DataField="CompanyName" HeaderText="Company name" SortExpression="CompanyName" />
    </Columns>
    <PagerTemplate>
        <asp:Button ID="Previous" runat="server" CommandName="Previous" Text="<"/>
        <asp:Button ID="Next" runat="server" CommandName="Next" Text=">"/>
        <asp:Button ID="Refresh" runat="server" CommandName="Refresh" Text="Refresh" />
        <asp:Button ID="Update" runat="server" CommandName="Update" Text="Update" />
    </PagerTemplate>
</asp:GridView>
4

3 回答 3

1

在您的 Gridview Cell 中定义一个容器,如 asp 面板。并将控件添加到容器而不是直接添加到 Table Cell。

于 2013-09-23T14:09:50.153 回答
0

不要DataBind在gridview之前调用代码。从那里删除代码并调用内部RowDataBound事件
尚未测试它可能有一些错误。

void GVR_RowDataBound(Object sender, GridViewRowEventArgs e)
  {    
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
       string CellContent1 = TC.Text;                        
        DropDownList DDL = new DropDownList();                        
        foreach (object s in Enum.GetValues(typeof(Ticket_Status))) 
        {
            DDL.Items.Add(s.ToString());
        }
        DDL.SelectedValue = CellContent1;
        DDL.ID = "I_Status_CB";    
e.Row.Cells[2].Controls.Add(DDL);    

    }    
  }
于 2013-09-23T14:20:18.740 回答
0

在您的网格视图中使用 ASP.NETPlaceHolder控件,如下所示:TemplateFieldItemTemplate

<asp:GridView ID="GridView1" runat="server" 
              OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Countries">
            <ItemTemplate>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server">
                </asp:PlaceHolder>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

现在在您的代码中,您可以“找到”PlaceHolder1并添加您想要的任何内容。我建议在RowDataBound事件中这样做,如下所示:

protected void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    // Only work with data rows, ignore header or footer rows
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        // Put logic here to find the placeholder and add controls to it
    }
}
于 2013-09-23T14:15:57.653 回答