Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET C#can';t从GridView tablecell中以编程方式添加的控件中检索信息_C#_Asp.net_Gridview_Tablecell_Generated - Fatal编程技术网

ASP.NET C#can';t从GridView tablecell中以编程方式添加的控件中检索信息

ASP.NET C#can';t从GridView tablecell中以编程方式添加的控件中检索信息,c#,asp.net,gridview,tablecell,generated,C#,Asp.net,Gridview,Tablecell,Generated,我在一个小项目上工作,我用控件在gridview中以编程方式切换文本 下面是代码的步骤: private GridViewRow EnterEditMode(GridView GV, int RowIndex) { GridViewRow GVR = GV.Rows[RowIndex]; foreach (TableCell TC in GVR.Cells) { if (TC == GVR.Cells[2]) {

我在一个小项目上工作,我用控件在gridview中以编程方式切换文本

下面是代码的步骤:

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);
        }
    } 
}
<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>
现在问题来了:

我正在创建一个控件,并用创建的控件替换tablecell中的文本。
问题是我找不到一种方法从创建的控件中获取值
我尝试了我所知道的每一种方法,我尝试对一行、整个表和单元格本身使用FindControl()normal和recursive

如果你能发现问题,或者你对这个问题有什么想法,请回复

友好问候,

-雷波

编辑:

看来我没有给你足够的信息来解决这个问题
我做的第一件事是将SQL数据库中的信息绑定到gridview中声明的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代码:

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);
        }
    } 
}
<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>


在Gridview单元格内定义一个容器,如asp面板。并将控件添加到容器中,而不是直接添加到表格单元格中。

在Gridview单元格中定义一个容器,如asp面板。并将控件添加到容器中,而不是直接添加到表单元格中。

在网格视图的
模板字段
项模板
中使用ASP.NET
占位符
控件,如下所示:

<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>
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
    }
}

在网格视图的
TemplateField
ItemTemplate
中使用ASP.NET
占位符
控件,如下所示:

<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>
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
    }
}

在调用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);    

    }    
  }

在调用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);    

    }    
  }

在GridView的RowDataBound事件中应称为“代码的DropDownList部分”。在GridView的RowDataBound事件中应称为“代码的DropDownList部分”。很抱歉,我忘了补充一点,当按钮触发时,DropDownList的创建被激活,我不相信使用RowDataBound会解决这个问题:我已经编辑了我的帖子并添加了更多关于表格本身的信息。很抱歉,我忘了添加一点,即当按钮被触发时,DropDownList的创建会被激活,我认为使用RowDataBound解决这个问题是行不通的:我已经编辑了我的帖子,并添加了更多关于表本身的信息。