将AutoPostBack添加到Gridview中作为文字插入的RadioButton

时间:2011-02-21 13:22:47

标签: asp.net gridview radio-button

我有一个带有RadioButton列的gridview,我按照以下文章创建了它:

http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-radio-buttons-cs

gridview代码是:

<asp:GridView ID="gvCounts" runat="server" Width="400px" AutoGenerateColumns="false"
        cssClass="Grid" OnRowCreated="gvCounts_RowCreated">            
        <Columns>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Literal ID="RadioButtonMarkup" runat="server"></asp:Literal>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Category" HeaderText="Category" />
            <asp:BoundField DataField="Subcategory" HeaderText="Subcategory" />
            <asp:BoundField DataField="Count" HeaderText="Count" />
            </Columns>
        </asp:GridView>

后端代码是:

 protected void gvCounts_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
            output.Text = string.Format(@"<input type='radio' name='CategoryGroup' id='RowSelector{0}' value='{0}' />", e.Row.RowIndex);
        }
    }

然而 - 我希望每次更改单选按钮时表单都会回发,而不是必须单击按钮才能“提交”。

由于这里的单选按钮不是.net控件,而是标准的HTML单选按钮,我不知道如何做到这一点。

3 个答案:

答案 0 :(得分:0)

asp.net单选按钮在浏览器中呈现如下:

<input id="RadioButton1" type="radio" name="RadioButton1" value="RadioButton1" onclick="javascript:setTimeout('__doPostBack(\'RadioButton1\',\'\')', 0)" />

你所要做的就是复制它。 :)

答案 1 :(得分:0)

我自己也遇到过同样的问题(并且正在使用相同的教程)。如果您使用jquery,我的解决方案可能适合您。在rowCreated函数中我有这个:

int index = e.Row.RowIndex;
Literal output = (Literal)e.Row.FindControl("RadioButtonMarkip");
output.Text=string.Format("<input type=\"radio\" name=\"Default_Group\" " +   "id=\"RowSelector{0}\" value=\"{0}\" ", e.Row.RowIndex); 

//check radio button if selected before
if (DefaultSeletectedIndex == e.Row.RowIndex || (!Page.IsPostBack && e.Row.RowIndex==0))
{
 output.Text += "checked=\"checked\"";
}

output.Text += "onclick = \"jQuery.fn.post_Default("+index+")\" />";

然后在JS方面:

<script type="text/javascript">
jQuery.fn.post_Default=function(){
$.post("YourPage.aspx", {Default: arguments[0]});
};
</script>

现在您需要的是页面加载,以检查Request.Form [“Default”]是否为您选中的单选按钮的索引值。这可能不是最优雅的方式,但到目前为止它的工作。我也想要使用jquery的$ .ajax函数来实现它。我认为这可能会更清洁,但现在这种方法很有效。

答案 2 :(得分:0)

  

代码背后:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gvCounts.DataSource = new SupplierRepository().GetSupplierList();
            gvCounts.DataBind();
        }
        else
        {
            string targetCtrl = Request.Params.Get("__EVENTTARGET");

            **// your method, do exactly what button click did
            ShowDetails();**
        }
    }
    protected void gvCounts_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
            output.Text = string.Format("<input type='radio' name='CategoryGroup' onclick='__doPostBack(\"RowSelector{0}\",\"\")' id='RowSelector{0}' value='{0}'  />", e.Row.RowIndex);
        }
    }