开发者

Add AutoPostBack to RadioButton in Gridview that is inserted as a literal

I have a gridview with a RadioButton column, which I am creating as per the following article:

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

The gridview code is:

<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="Catego开发者_如何学运维ry" HeaderText="Category" />
            <asp:BoundField DataField="Subcategory" HeaderText="Subcategory" />
            <asp:BoundField DataField="Count" HeaderText="Count" />
            </Columns>
        </asp:GridView>

And the back-end code is:

 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);
        }
    }

However - I want the form to Post Back every time the radio button is changed, as opposed to having to click a button to "submit".

As the radio buttons here are not .net controls, but standard HTML radio buttons, I'm not sure how I can do this.


An asp.net radio button renders like this in the browser:

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

All you have to do is replicate it. :)


Code Behind:

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);
        }
    }


I've been having the same issue myself (and was using the same tutorial). If you use jquery, my solution might work for you. In the rowCreated function I have this:

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+")\" />";

Then on the JS side:

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

Now all you need is the page load to check Request.Form["Default"] for the index value of your checked radio button. This may not be the most elegant way of doing it, but so far its working. I am also toying with the idea of just using jquery's $.ajax function to do it. I think that might be a bit cleaner, but for now this works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜