入力フィールドにデータを入力するメソッドがあります:
private void populateMostRecentStory(string selectedUser)
{
recentPK = getRecentStoryPK(selectedUser);
if (Int32.Parse(recentPK) != -1)
{
string[] returnedValues = retrieveLastStory(recentPK);
if (returnedValues[0] != null)
{
int i = 0;
int x = 0;
foreach (var item in catagoryDropDown.Items)
{
if (item.ToString().Equals(returnedValues[0].ToString()))
{
catagoryDropDown.SelectedIndex = i;
break;
}
i++;
}
foreach (var item in applicationDropDown.Items)
{
if (item.ToString().Equals(returnedValues[2].ToString()))
{
applicationDropDown.SelectedIndex = x;
break;
}
x++;
}
dateInput.SelectedDate = DateTime.Parse(returnedValues[1]);
incidentInput.Text = returnedValues[3];
hoursInput.Text = returnedValues[5];
descriptionInput.Text = returnedValues[6];
}
}
else
{
clearStoryData();
}
dataBindFields();
}
private void dataBindFields()
{
catagoryDropDown.DataBind();
dateInput.DataBind();
applicationDropDown.DataBind();
incidentInput.DataBind();
hoursInput.DataBind();
descriptionInput.DataBind();
}
ドロップダウンメニューを変更するためのアクションイベントが発生したときにうまく機能します:
//user drop down
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
populateMostRecentStory(userNameDropDown.SelectedItem.Value);
}
ただし、ページの読み込み時には、categoryDropDown と applicationDropDown を除くすべてのフィールドが正しく入力されます。
protected void Page_Load(object sender, EventArgs e)
{
populateMostRecentStory(getPKofUserLoggedIn());
//Assign SQL parameter to user drop down list so that the list shows the logged in user first followed by other users in alphabetical order
SqlDataSource1.SelectParameters["userLoggedIn"].DefaultValue = User.Identity.Name;
}
ページの読み込み時に userNameDropDown.SelectedItem.Value が NullExceptionError になるため、別の方法を使用して主キーを取得する必要がありました。事前に DataBind を試みましたが、うまくいきません。
とにかく、ページの読み込み時に getPKofUserLoggedIn() の値を出力すると: 45 になり、userNameDropDown.SelectedItem.Value を選択してアクション イベントが発生したときに値を出力すると、それも 45 になります。
私は何を間違っていますか?