我在网上浪费了3-4个小时后才找到解决问题的方法。
现在我有一个转发器A,里面有一个下拉,一个TextBox和一个LinkButton,说添加更多。
单击该按钮,转发器的数据将保存到ViewState中,并添加一个新行并恢复ViewState数据。
现在,虽然恢复了TextBox中的数据,但下拉列表始终设置为空白。
aspx页面
<asp:Repeater runat="server" ID="POTaxDetails" OnItemCreated ="POTaxDetails_OnItemCreated" OnItemCommand="POTaxDetails_OnItemCommand" >
<HeaderTemplate>
<tr>
<td colspan="2" class="tdDataDisp">
</td>
<td class="tdDataDisp">
<asp:Label ID="LbTaxTypeText" runat="server" Text="Tax Type" CssClass="LbTblDataDisp"
Font-Bold="true"></asp:Label>
</td>
<td class="tdDataDisp">
<asp:Label ID="LbTaxAmount" runat="server" Text="Tax Amount" CssClass="LbTblDataDisp"
Font-Bold="true"></asp:Label>
</td>
<td>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td colspan="2" class="tdDataDisp">
<td class="tdDataDisp">
<asp:DropDownList ID="TaxTypeList" runat="server" CssClass="LbTblDataSelect" OnDataBinding="TaxTypeList_OnDataBinding" AutoPostBack="true" >
<asp:ListItem Value="">----Select----</asp:ListItem>
</asp:DropDownList>
</td>
<td class="tdDataDisp">
<asp:TextBox ID="TaxAmount" runat="server" amtType="baseamount" expID="" taxType=""
CssClass="tblDataInput"></asp:TextBox>
</td>
<td class="tdDataDisp">
<asp:LinkButton ID="btnRemove" runat="server" CommandName="remove" >Remove This</asp:LinkButton>
<asp:LinkButton ID="btnAddMore" runat="server" CommandName="addmore" >Add More</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
cs code
protected void POTaxDetails_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
//Repeater rp = e.CommandSource
RepeaterItem rpItem = e.Item as RepeaterItem;
Repeater rp = rpItem.NamingContainer as Repeater;
if (e.CommandName == "addmore")
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
DropDownList ddl = rp.Items[rowIndex].FindControl("TaxTypeList") as DropDownList;
TextBox tb = rp.Items[rowIndex].FindControl("TaxAmount") as TextBox;
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["TaxType"] = ddl.SelectedValue;
//taxlist = "";
if (taxlist == "")
{
taxlist = "'" + ddl.SelectedValue + "'";
}
else
{
taxlist += "," + "'" + ddl.SelectedValue + "'";
}
dtCurrentTable.Rows[i - 1]["TaxAmount"] = tb.Text;
// ClientScript.RegisterStartupScript(GetType(), "hwa", "alert('" + dtCurrentTable.Rows[i - 1]["TaxType"] + " - Hello World');", true);
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
rp.DataSource = dtCurrentTable;
rp.DataBind();
}
}
rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList taxType = (DropDownList)rp.Items[rowIndex].FindControl("TaxTypeList");
TextBox tb = (TextBox)rp.Items[rowIndex].FindControl("TaxAmount");
//Disable previous rows
if (i < dt.Rows.Count - 1)
{
taxType.Enabled= false;
tb.Enabled = false;
}
else
{
taxType.Enabled = true;
tb.Enabled = true;
}
taxType.SelectedValue = dt.Rows[i]["TaxType"].ToString();
ClientScript.RegisterStartupScript(GetType(), "hwa", "alert('" + dt.Rows[i]["TaxType"].ToString() + " - Bye World');", true);
tb.Text = dt.Rows[i]["TaxAmount"].ToString();
rowIndex++;
}
}
}
}
}
protected void TaxTypeList_OnDataBinding(object sender, System.EventArgs e)
{
DropDownList ddl1 = (DropDownList)(sender);
var firstitem = ddl1.Items[0];
ddl1.Items.Clear();
ddl1.Items.Add(firstitem);
String selectCmd = "";
if (taxlist == "")
{
selectCmd = "SELECT * from T_Tax_Type";
}
else
{
selectCmd = "SELECT * from T_Tax_Type where TaxID not in (" + taxlist + ")";
}
Repeater rp = ddl1.NamingContainer.NamingContainer as Repeater;
int cnt = rp.Items.Count;
SqlCommand myCommand = new SqlCommand(selectCmd, myConnection);
SqlDataReader reader;
reader = myCommand.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
ddl1.Items.Add(new ListItem(reader.GetString(1), reader.GetString(0)));
}
}
reader.Close();
ddl1.SelectedValue = "";
}
protected void bindTaxList(Repeater rp, string taxList)
{
try
{
DataTable dt = new DataTable();
DataRow dr = null;
//Create DataTable columns
dt.Columns.Add(new DataColumn("TaxType", typeof(string)));
dt.Columns.Add(new DataColumn("TaxAmount", typeof(string)));
// dt.Columns.Add(new DataColumn("Comments", typeof(string)));
dr = dt.NewRow();
dr["TaxType"] = string.Empty;
dr["TaxAmount"] = string.Empty;
dt.Rows.Add(dr);
rp.DataSource = dt;
rp.DataBind();
ViewState["CurrentTable"] = dt;
}
catch (Exception ex)
{
}
}
现在我认为只有前两个函数之间存在一些问题。第三个功能是我第一次绑定转发器的方式。