我无法在我的应用中预先填充CheckBoxList3。我已成功从数据库中检索字符串数据并传递给数组,但例程失败:
NullReferenceException:Object not set to instance of an object error at line:
ListItem currentCheckBox = cb3.Items.FindByValue(items[i].ToString());
readMyString()方法使用SqlDataReader从SQL表中读取和拆分字符串列,然后将值例如:“A,B,C”传递给字符串数组,以标识应选择6个复选框中的哪一个。代码参考:[http://mikesdotnetting.com/Article/53/Saving-a-user's-CheckBoxList-selection-and-rempolulation-the-CheckBoxList-from-saved-data]
HTML CODE:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="MyID"
DataSourceID="sdsmyTable1" ForeColor="#333333" GridLines="None"
onrowdeleted="GridView1_RowDeleted"
onrowdeleting="GridView1_RowDeleting"
onrowupdated="GridView1_RowUpdated"
onrowupdating="GridView1_RowUpdating"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="myID" HeaderText="myID" ReadOnly="True"
SortExpression="myID" />
<asp:BoundField DataField="Date1" HeaderText="Date driver lic issued"
SortExpression="Date1" />
<asp:TemplateField HeaderText="chooseOne" SortExpression="chooseOne">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("chooseOne") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="RadioButtonList2" runat="server" SelectedValue='<%# Bind("chooseOne") %>' Font-Size="Small"
RepeatDirection="Horizontal">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MyCommaSeparatedString" SortExpression="MyCommaSeparatedString">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("MyCommaSeparatedString") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MyCommaSeparatedString") %>'></asp:TextBox>
<asp:CheckBoxList ID="CheckBoxList3" runat="server" Font-Size="Small"
RepeatDirection="Horizontal">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
<asp:ListItem Value="E">E</asp:ListItem>
<asp:ListItem Value="F">F</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
C#代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//// establish a datarow to dig into its minutia
DataRowView drv = e.Row.DataItem as DataRowView;
// if 1: RowType of gridview control is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//if 2: is datarow in edit mode
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
RadioButtonList rb2 = (RadioButtonList)e.Row.FindControl("RadioButtonList2");
CheckBoxList cb3 = (CheckBoxList)e.Row.FindControl("CheckBoxList3");
//CheckBoxList cb3 = (CheckBoxList)GridView1.Rows[GridView1.EditIndex].FindControl("CheckBoxList3");
CheckBoxList cb4 = (CheckBoxList)e.Row.FindControl("CheckBoxList4");
// Create an instance of SqlConn class
SqlConnClass getMyStr= new SqlConnClass();
//Return datareader to read Endorsements column from database
SqlDataReader rdr = getMyStr.selectMyString(GetMyID());
try
{
// column
if (rb2 != null)
{
rb2.SelectedValue = drv[2].ToString();
}
// column
//CheckBoxList cb3 = (CheckBoxList)e.Row.FindControl("CheckBoxList3");
if (cb3 != null)//if (cb3 contains 6 checkboxes (I, N, H, X, T, K)
{
//bind checkbox manually
cb3.DataSource = sdsDatasource;
cb3.DataTextField = "MyCommaSeparatedString ";
cb3.DataValueField = "MyCommaSeparatedString ";
cb3.DataBind();
// if row exists
if (rdr.HasRows)//(dt.Rows.Count > 0 && dt != null)
{
//start reading
rdr.Read();
//extract comma separated string from datareader into one-dimensional array
string[] items = rdr.GetString(0).Split(',');
//returns the upper bound for the indexes of the first dimension of the Array
for (int i = 0; i <= items.GetUpperBound(0); i++)
{
// currentCheckBox IS WHERE NULL OCCURS:
ListItem currentCheckBox = cb3.Items.FindByValue(items[i].ToString());
if (currentCheckBox != null)
{
currentCheckBox.Selected = true;
}
//cb3.SelectedValue = drv[3].ToString();
//}
}// end of for
//close SqlDataReader
rdr.Close();
}
// column
//CheckBoxList cb4 = (CheckBoxList)e.Row.FindControl("CheckBoxList4");
if (cb4 != null)
{
cb4.SelectedValue = drv[4].ToString();
}
}// end of if
}// end of try
catch (IndexOutOfRangeException ex2)
{
System.Diagnostics.Debug.WriteLine(ex2.Message + "; " + ex2.Source + "; " + ex2.TargetSite);
}//end of catch
}// end of if (rowstate)
} // end of if (rowtype)
}// end of method
我尝试过:从GridView1_ RowDataBound 事件和/或从GridView1_ RowEditing 事件调用readMyString()方法。
由于CheckBoxList3开头为空并且似乎不想为null,我添加了行cb3.Items[i].Value = items[i];
,但错误只是移动到新行。我一定错过了什么。
谁能看到我的错误?我必须把代码放在错误的方法中......?
答案 0 :(得分:0)
您正在尝试在GridView中找到该控件。控件位于GridView Rows中。 所以你需要做这样的事情:
CheckBoxList cb3 = (CheckBoxList) GridView1.Rows[GridView1.EditIndex].FindControl("CheckBoxList3");
修剪下面的项目[i]:
ListItem currentCheckBox = cb3.Items.FindByValue(items[i].Trim());