ラジオボタンリストとテキストエリアのあるページがあります。データは、ユーザーの選択に基づいてテキストエリア内に動的に表示されます。また、OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" を設定して、ユーザーが記事を参照できるようにする URL を作成します (ラジオ ボタンの選択)。
作成した URL (つまり、 http://test.com/test.aspx?selected=3 ) を新しいブラウザーにカット アンド ペーストする以外はすべて機能します。コードは radiobuttonlist1.selectedindex を -1 に割り当て続けます。
だから、ここに私がデバッグモードで見ているものがあります
ケース 1 URL をカット アンド ペーストして新しいブラウザhttp://test.com/test.aspx?selected=1に貼り付けると、page_load メソッド コード RadioButtonList1.SelectedIndex の最後で = -1 になります。何らかの理由で、selectindex が正しく割り当てられていません。
ケース 2起動した Web ページ内でラジオ ボタンを選択すると、 post back が true であるため、page_load コードがスキップされます。次に、RadioButtonList1_SelectedIndexChanged 内に URL を作成します。次に、ページ読み込みメソッドを実行し、最後に正しい RadioButtonList1.SelectedIndex 値を保持します。
ケース 3 http://test.com/test.aspx?selected=2 を指すことを使用している、起動された Web ページ内のリンクを選択すると、ポストバックが false になるため、page_load コードをループし、正しい RadioButtonList1.SelectedIndex を正常に保持します。最後に値。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int selected;
if (Request.QueryString["selected"] != null)
{
if (int.TryParse(Request.QueryString["selected"], out selected))
{
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
else
{
int firstart = 0;
RadioButtonList1.SelectedIndex = firstart;
}
}
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
try{
e.Command.Parameters["@URL_FK"].Value = Session["URL_PK"];
}
catch (Exception ex)
{
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "test.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
}