troubleshooting Question

Lack of understanding with AsyncPostback concept and no success with Variables in T-SQL for SqlDataSource ASP.net in C

Avatar of Nate_LR
Nate_LR asked on
JavaScriptASP.NETMicrosoft SQL Server 2008
5 Comments1 Solution155 ViewsLast Modified:
I'm working on an interactive map that combines Asp.net in C and the Google Maps Api v3.
 Here is my code....

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%--<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>--%>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

<style type="text/css">
     
  #map_canvas  
  {
    width:570px; 
    height:596px;
    border: 2px solid #660000;
  }
   
   
   #msgResult 
   {
     color:Red;
     font-weight:bold;
     height: 25px;
   }
   
</style>


<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="Scripts/googleMap.js"></script>

<script type="text/javascript">

    function fillBorderTwnRngLstBox(tr, trid) {

        var trLstBx = document.getElementById("MainContent_lstbxBorderingTwnRng");
        var el = document.createElement("option");
        el.textContent = tr;
        el.value = trid;
        trLstBx.appendChild(el);
    }


    function clearBorderTwnRngLstBox() {

        var trLstBx = document.getElementById("MainContent_lstbxBorderingTwnRng");
        trLstBx.innerHTML = "";
    }


    function displayResultMsg(msg) {

        msgSpan = document.getElementById("msgResult");
        msgSpan.innerHTML = msg;
    }


    function clearForm() {        

        cList = document.getElementById("MainContent_CountyList");
        cList.value = 0;
        triggerEvent(cList, 'onchange');

        tList = document.getElementById("MainContent_TwnList");
        tList.value = 0;
        triggerEvent(tList, 'onchange');

        rList = document.getElementById("MainContent_RngList");
        rList.value = 0;
        triggerEvent(rList, 'onchange');

        sList = document.getElementById("MainContent_SectionList");
        sList.value = 0;
        triggerEvent(sList, 'onchange');

        displayResultMsg("");
    }


    function triggerEvent(el, type) {
        if ((el[type] || false) && typeof el[type] == 'function') {
            el[type](el);
        }
    }


</script>

</asp:Content>




<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <asp:ScriptManager ID="ScriptManager1" runat="server">
             </asp:ScriptManager>

   <%--<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>--%>

    
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <Triggers>
             <asp:AsyncPostbackTrigger ControlID="CountyList" EventName="SelectedIndexChanged" />
             <asp:AsyncPostbackTrigger ControlID="TwnList" EventName="SelectedIndexChanged" />
             <asp:AsyncPostbackTrigger ControlID="RngList" EventName="SelectedIndexChanged" />
             <asp:AsyncPostbackTrigger ControlID="SectionList" EventName="SelectedIndexChanged" />
             <asp:AsyncPostbackTrigger ControlID="btnReset" EventName="Click" />
        </Triggers>        
    </asp:UpdatePanel>
    
    <table>
    <tr>            
    <td>
        County:
        <asp:DropDownList ID="CountyList" runat="server" DataSourceID="CountyDataSource" 
                DataTextField="CountyName" DataValueField="FIPSID"  
                AppendDataBoundItems="True" EnableViewState="True" AutoPostBack="True"
                OnSelectedIndexChanged="CTRSList_SelectedIndexChanged">
                <asp:ListItem Value="0" Text=""></asp:ListItem>
        </asp:DropDownList>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>

    <td>
        Township:
        <asp:DropDownList ID="TwnList" runat="server" DataSourceID="TwnDataSource" 
                DataTextField="TWN" DataValueField="TOWNSHIPID" 
                AppendDataBoundItems="True" EnableViewState="True" AutoPostBack="True"
                OnSelectedIndexChanged="CTRSList_SelectedIndexChanged">
                <asp:ListItem Value="0" Text=""></asp:ListItem>
        </asp:DropDownList>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </td>

    <td>
        Range:
        <asp:DropDownList ID="RngList" runat="server" DataSourceID="RngDataSource" 
                DataTextField="RNG" DataValueField="RANGEID" 
                AppendDataBoundItems="True" EnableViewState="True" AutoPostBack="True"
                OnSelectedIndexChanged="CTRSList_SelectedIndexChanged">
                <asp:ListItem Value="0" Text=""></asp:ListItem>
        </asp:DropDownList>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;         
    </td>

    <td>
        Section:
        <asp:DropDownList ID="SectionList" runat="server" DataSourceID="SecDataSource" 
                DataTextField="SECTION" DataValueField="SECID" 
                AppendDataBoundItems="True" EnableViewState="True" AutoPostBack="True"
                OnSelectedIndexChanged="CTRSList_SelectedIndexChanged">
                <asp:ListItem Value="0" Text=""></asp:ListItem>
        </asp:DropDownList>
                
    </td>
    </tr>

    <tr>
    <td colspan="2" align="left">
        
        <div id="msgResult"></div>
        
    </td>
    <td colspan="2" align="right">
        
        <asp:Button ID="btnReset" runat="server" OnClick="btnReset_Click" Text="Reset" />
        
    </td>
    </tr>    
    </table>



    <table>
    <tr>
    <td>
        <div id="map_canvas">        
        </div>
    </td>
    <td>
        <asp:ListBox ID="lstbxBorderingTwnRng" runat="server"  
             Width="100px" AutoPostBack="True"></asp:ListBox>
    </td>
    </tr>
    </table>
    



    <asp:SqlDataSource ID="CountyDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT FIPSID, CountyName
                        FROM tblCounty
                        WHERE FIPSID NOT IN (998,9999)
                        ">
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="TwnDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT TOWNSHIPID, TWN
                        FROM tblTownship
                        WHERE TOWNSHIPID NOT IN (44,45,46,47,48,57,58,115,116)
                        ORDER BY TWN
                        ">
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="RngDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT RANGEID, RNG
                        FROM tblRange
                        WHERE RANGEID NOT IN (49,50,51,52,53,54,55,56,57)
                        ORDER BY RNG
                        ">
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="SecDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT SECID, SECTION FROM tblSection WHERE SECID <= 36">
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="dsGetCTR" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT ID, f_TR_id  FROM [tblBoundary] 
                        WHERE ([f_county_id] = @ctyID)
                        AND ([f_TR_id] = (SELECT TRID from tblTR WHERE T_ID = @twnID AND R_ID = @rngID))
                        AND ([f_sec] = 173)
                        AND ([f_quad_id] = 9999)
                        ">
        <SelectParameters>            
            <asp:Parameter Name="ctyID" />
            <asp:Parameter Name="twnID" />
            <asp:Parameter Name="rngID" />
        </SelectParameters>
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="dsGetCTRS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT ID, f_TR_id  FROM [tblBoundary] 
                        WHERE ([f_county_id] = @ctyID)
                        AND ([f_TR_id] = (SELECT TRID from tblTR WHERE T_ID = @twnID AND R_ID = @rngID))
                        AND ([f_sec] = @secID)
                        AND ([f_quad_id] = 9999)
                        ">
        <SelectParameters>            
            <asp:Parameter Name="ctyID" />
            <asp:Parameter Name="twnID" />
            <asp:Parameter Name="rngID" />
            <asp:Parameter Name="secID" />
        </SelectParameters>
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="dsGetTR" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT ID, f_TR_id  FROM [tblBoundary] 
                        WHERE ([f_county_id] = 9999)
                        AND ([f_TR_id] = (SELECT TRID from tblTR WHERE T_ID = @twnID AND R_ID = @rngID))
                        AND ([f_sec] = 173)
                        AND ([f_quad_id] = 9999)
                        ">
        <SelectParameters>
            <asp:Parameter Name="twnID" />
            <asp:Parameter Name="rngID" />
        </SelectParameters>
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="dsGetTRS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT ID, f_TR_id  FROM [tblBoundary] 
                        WHERE ([f_county_id] = 9999)
                        AND ([f_TR_id] = (SELECT TRID from tblTR WHERE T_ID = @twnID AND R_ID = @rngID))
                        AND ([f_sec] = @secID)
                        AND ([f_quad_id] = 9999)
                        ">
        <SelectParameters>
            <asp:Parameter Name="twnID" />
            <asp:Parameter Name="rngID" />
            <asp:Parameter Name="secID" />
        </SelectParameters>
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="dsGetCty" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT ID  FROM [tblBoundary] 
                        WHERE ([f_county_id] = @ctyID)
                        AND ([f_TR_id] = 1576)
                        AND ([f_sec] = 173)
                        AND ([f_quad_id] = 9999)
                        ">
        <SelectParameters>            
            <asp:Parameter Name="ctyID" />
        </SelectParameters>
    </asp:SqlDataSource>


    <asp:SqlDataSource ID="dsGetBorderingTwnRngList" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="spGetBorderingTwnRng" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:Parameter Name="trID" />            
        </SelectParameters>
    </asp:SqlDataSource>


   </asp:Content>
   

Here is my code behind (which has been trimmed down)....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Google Maps Initialization",
                      string.Format("initializeMap();"), true);
        }
        //else
        //{
        //    errorLabel.Text = "After postback";
        //}
    }


    

    protected void CTRSList_SelectedIndexChanged(object sender, EventArgs e)
    {        
        //errorLabel.Text = ""; //This doesn't work!!
        ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
        var ctyID = CountyList.SelectedValue;
        var twnID = TwnList.SelectedValue;
        var rngID = RngList.SelectedValue;
        var secID = SectionList.SelectedValue;
        
       
         if (ctyID == "0" & twnID != "0" & rngID != "0" & secID == "0") // Township and Range
        {
            //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "TR",
            //         "displayResultMsg('TR');", true);
            
            dsGetTR.SelectParameters["twnID"].DefaultValue = twnID;
            dsGetTR.SelectParameters["rngID"].DefaultValue = rngID;

            DataView dvBnd = (DataView)dsGetTR.Select(DataSourceSelectArguments.Empty);
            DataTable dtBnd = dvBnd.ToTable();

            if (dtBnd.Rows.Count > 1)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Multiple BndIDs",
                     "displayResultMsg('Multiple BndIDs!');", true);

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Clear Map",
                     "clearMap();", true);
            }
            else if (dtBnd.Rows.Count == 0)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Location Does Not Exist",
                     "displayResultMsg('Location Does Not Exist!');", true);

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Clear Map",
                     "clearMap();", true);
            }
            else
            {
                //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CTR",
                //         "displayResultMsg(" + dtBnd.Rows[0][0].ToString() + ");", true);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "",
                         "displayResultMsg('');", true);

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Clear Section",
                         "clearSection();", true);

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Clear County",
                         "clearCounty();", true);

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Get Township Polygon",
                         "getTownship(" + twnID + ", " + rngID + ");", true);

                DataRowView row = (DataRowView)dvBnd[0];
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CTR",
                         "displayResultMsg(" + row["f_TR_id"].ToString() + ");", true);
                dsGetBorderingTwnRngList.SelectParameters["trID"].DefaultValue = row["f_TR_id"].ToString();
                DataView dvBTrBnd = (DataView)dsGetBorderingTwnRngList.Select(DataSourceSelectArguments.Empty);
                DataTable dtBTrBnd = dvBTrBnd.ToTable();
                //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CTR",
                //         "displayResultMsg(" + dtBTrBnd.Rows.Count.ToString() + ");", true); //dtBTrBnd.Rows[0][0].ToString()

                //lstbxBorderingTwnRng.Items.Clear();
                //lstbxBorderingTwnRng.Rows = dtBTrBnd.Rows.Count;

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Clear List Box",
                         "clearBorderTwnRngLstBox();", true);

                foreach (DataRow trRow in dtBTrBnd.Rows)
                {
                    //lstbxBorderingTwnRng.Items.Add(trRow["TRID"].ToString());
                    //fillBorderTwnRngLstBox(tr, trid);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "TR List Box",
                         "fillBorderTwnRngLstBox(" + trRow["TRID"].ToString() + ", " + trRow["TRID"].ToString() + ");", true);
                    //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert",
                         //"window.alert(" + trRow["TRID"].ToString() + ");", true); 
                }

                //lstbxBorderingTwnRng.DataSource = dtBTrBnd; //dtBTrBnd.DefaultView 
                //lstbxBorderingTwnRng.DataTextField = "TRID"; //twnRng,  dtBTrBnd.Columns["TRID"].ToString()
                //lstbxBorderingTwnRng.DataValueField = "TRID";
                               
                ////lstbxBorderingTwnRng.DataMember = "TRID";
                //lstbxBorderingTwnRng.DataBind();
            }
              
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "No Can Do",
                     "displayResultMsg('');", true);
        }
        
    }


    protected void btnReset_Click(object sender, System.EventArgs e)
    {        
        ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
        //CountyList.ClearSelection(); //Doesn't work!
        //CountyList.SelectedIndex = -1; //Doesn't work!       
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Clear Form",
                     "clearForm();", true);

        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Clear Map",
                     "clearMap();", true);
    }  

}

Problem 1:
In order to get this to work correctly I have an UpdatePanel with AsyncPostbackTriggers, primarily for drop-down menus.   I'm finding this AsyncPostback concept to be a pain to deal with and don't really understand why it's needed.   I've had to resort  to using javascript to get some things to work, such as reset the drop-down menus, display messages, and fill a listbox.   I don't understand why this won't work...
CountyList.ClearSelection();
or this....
errorLabel.Text = "";
and especially this....
lstbxBorderingTwnRng.DataSource = dtBTrBnd;
lstbxBorderingTwnRng.DataTextField = "TRID";
lstbxBorderingTwnRng.DataValueField = "TRID";
lstbxBorderingTwnRng.DataBind();
Why must I resort to javascript?

Problem 2:
For this map I'd like to not only show the selected Township and Range on the map, but I'd also like to show the township polygons that border the selected one.  As mentioned in problem 1, I've had to use javascript to do this.   But there appears to be a problem with either using a declared variable or concatenating columns and using 'AS' with a sqldatasource but I'm not sure which it is.   Here is my stored procedure...
ALTER PROCEDURE [dbo].[spGetBorderingTwnRng] 
	-- Add the parameters for the stored procedure here
	@trID As int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	DECLARE @tr As Geometry
    SET @tr = (SELECT TR_geom FROM tblTR WHERE TRID = @trID)

    SELECT TRID, TWN + ' ' + RNG AS twnRng FROM tblTR 
    INNER JOIN tblTownship ON tblTR.T_ID = tblTownship.TOWNSHIPID 
    INNER JOIN tblRange ON tblTR.R_ID = tblRange.RANGEID
    WHERE TR_geom.STTouches(@tr) = 1
END
I should return about 7 townships and using this, dtBTrBnd.Rows.Count, confirms that, but my listbox will only show 1 value.  Can anyone assist me please?
ASKER CERTIFIED SOLUTION
Kelvin McDaniel
Owner/Consultant/Sr. Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros