• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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

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 = "";
            }
        }
    }
}
0
searchsanjaysharma
Asked:
searchsanjaysharma
1 Solution
 
BuggyCoderCommented:
0
 
searchsanjaysharmaAuthor 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.
0
 
Jerry MillerCommented:
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
        }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jerry MillerCommented:
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    
0
 
cubaman_24Commented:
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

0
 
Jerry MillerCommented:
I didn't notice that you were in C#. It is in VB.Net where you have to use the Next keyword to increment to the next record.
0
 
searchsanjaysharmaAuthor Commented:
Thanks
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now