We help IT Professionals succeed at work.

How to redirect from the web page back to loop of Gridview.

searchsanjaysharma
on
425 Views
Last Modified: 2012-05-11
I have a gridview with one column prefixed with checbox.
1 button of Check all
1 button of uncheck all
1 button of send
I want when i click on send button the sms should go to all the rows which are checked.
But the following code is send the message to first mobile number only that was checked.
Can any1 help.

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class _Default : System.Web.UI.Page
{

    SqlDataAdapter da;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            bindzones();
        }

    }
    public void bindzones()
    {

        da = new SqlDataAdapter("SELECT * FROM PERSON ORDER BY 1,2", ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
    private void ToggleCheckState(bool checkState)
    {
     
        foreach (GridViewRow row in GridView1.Rows)
        {
     
            CheckBox cb = (CheckBox)row.FindControl("cbm");
            if (cb != null)
            {
                cb.Checked = checkState;
            }
        }
    }

    protected void btncheckall_Click(object sender, EventArgs e)
    {
        ToggleCheckState(true);
    }
    protected void btnuncheckall_Click(object sender, EventArgs e)
    {
        ToggleCheckState(false);
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
       
        string s;
        foreach (GridViewRow row in GridView1.Rows)
        {
            s = "http://xx.xx.xx.xxx:xxxx/WebSMS/SMSAPI.jsp?username=xxx&password=xxxxx&sendername=xxx&mobileno=";
            if (((CheckBox)row.FindControl("cbm")).Checked)
            {
                string i = Server.HtmlDecode(row.Cells[1].Text);
               
                s = s.Trim()+i.Trim()+"&message="+"Hi Mission Accomplished";
                Response.Redirect(s);
                s = "";
            }
        }
    }
}
Comment
Watch Question

Author

Commented:
No my question is the following function works only for the first value.How to make it work for all the values.
    protected void btnSend_Click(object sender, EventArgs e)
    {
       
        string s;
        foreach (GridViewRow row in GridView1.Rows)
        {
            s = "http://xx.xx.xx.xxx:xxxx/WebSMS/SMSAPI.jsp?username=xxx&password=xxxxx&sendername=xxx&mobileno=";
            if (((CheckBox)row.FindControl("cbm")).Checked)
            {
                string i = Server.HtmlDecode(row.Cells[1].Text);
               
                s = s.Trim()+i.Trim()+"&message="+"Hi Mission Accomplished";
                Response.Redirect(s);
                s = "";
            }
        }
    }

Here when Response.Redirect(s) works then it does not come back to the loop.
You are not incrementing to the next row, add the NEXT keyword like this:

Foreach (GridViewRow row in GridView1.Rows)
        {
     
            CheckBox cb = (CheckBox)row.FindControl("cbm");
            if (cb != null)
            {
                cb.Checked = checkState;
            }
next
        }
Sorry, I put it in the wrong place, it should be outside the brackets.

Foreach (GridViewRow row in GridView1.Rows)
        {
     
            CheckBox cb = (CheckBox)row.FindControl("cbm");
            if (cb != null)
            {
                cb.Checked = checkState;
            }
         }
  next    
Hello:
When you call Response.Redirect(s), the execution is transfered to the page passed as parameter, and current execution is stopped. Response.Redirect throws a ThreadInterruptedException in current thread to stop it, that's why only the first row is executed.
What you need is to call some function with the right parameters to send the sms, not to redirect the page.
protected void btnSend_Click(object sender, EventArgs e)
    {
       
        string s;
        foreach (GridViewRow row in GridView1.Rows)
        {
            // not needed s = "http://xx.xx.xx.xxx:xxxx/WebSMS/SMSAPI.jsp?username=xxx&password=xxxxx&sendername=xxx&mobileno=";
            if (((CheckBox)row.FindControl("cbm")).Checked)
            {
                string number = GetTargetNumber();
				SendSms(number);
            }
        }
    }
	
	private void SendSms(string target){
		// Custom code to send sms
	}

Open in new window

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.