我正在做一个小项目,并以编程方式在带有控件的网格视图中切换文本。
以下是代码的步骤:
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>