我正在开发一个小项目,我用编程方式在一个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);
}
}
}
现在问题出在这里:
我创建一个控件并用创建的控件替换表格单元格中的文本
问题是我找不到从创建的控件中获取值的方法!
我已经尝试过我所知道的每一种方法,我尝试使用FindControl()正常并递归一行,整个表格和单元格本身。
如果您发现问题或者您对此问题有任何想法,请回复!
友好的问候,
-Raybbo。
修改
似乎我没有给你足够的信息来解决这个问题。
我做的第一件事是将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代码:
<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>
答案 0 :(得分:1)
在Gridview Cell中定义一个容器,如asp面板。并将控件添加到容器而不是直接添加到表格单元格。
答案 1 :(得分:0)
在网格视图的PlaceHolder
TemplateField
中使用ASP.NET ItemTemplate
控件,如下所示:
<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
}
}
答案 2 :(得分:0)
请勿在{{1}} gridview之前调用代码。
从那里删除代码并在 DataBind
事件中调用
没有测试它可能会有一些错误。
RowDataBound