asp.net 2.0 gridview: selecting alphabet link (A-Z) and jumping to row where data starts with selected alphabet

Hello!

I have gridview. I want to make links in alphabet order on top of the gridview (from A-Z). When user clicks let say R the browser will jump to the first row where surname starts with R. besides R there will be link to the top of the page where is link selection.

I actually made post on asp.net forum:
http://forums.asp.net/t/1381397.aspx

here are the pictures. this is made in old asp and I need it in .net (c#).
http://img14.imageshack.us/img14/839/screenshot034ss9.png
http://img14.imageshack.us/img14/7319/screenshot035hv9.png

I really need this solved. I didn't find anything useful on the net. The gridview is quite big but i don't need alphabetic paging (I have search for this in other view). When user click appropirate letter link the browser scroll down to appropriate letter.Beside each letter is link to the top.

Thank you so much.





<asp:GridView ID="GridView2" runat="server" DataSourceID="sqlDataSource2" AutoGenerateColumns="false"  CssClass="headertable" DataKeyNames="ID"  OnRowCreated="GridView1_OnRowCreated" 
 OnRowUpdating="UpdateRecord2"  OnRowDataBound="GridView1_RowDataBound" >
 
<%--Style="border-color: #cccccc; border-width:2px; border-bottom-style:solid;"--%>
     <HeaderStyle  CssClass="headertr"/>
     <RowStyle  CssClass="itemtemplate"   />
    <AlternatingRowStyle  CssClass="alternatingitemtemplate" />
     <%--<SelectedRowStyle CssClass="selectedrowcolor" />--%>
    
     <Columns>
         <%--<asp:BoundField Visible="false" SortExpression="ID" DataField="ID"  />--%>
         <asp:BoundField DataField="pri" HeaderText="pri" SortExpression="pri" Visible="false" />           
         <asp:TemplateField HeaderText="Priimek in ime" SortExpression="pri" > 
            
            <ItemTemplate>
                <asp:Label runat="server" ID="lblS" Font-Size="11px" Text='<%#Eval("naz").ToString() + "&nbsp;" +  "<b>" + Eval("pri").ToString() + "&nbsp;" + Eval("im").ToString()+ "</b>" + "&nbsp;" + Eval("izobrazba").ToString() %>'></asp:Label>
            </ItemTemplate>
            
            <AlternatingItemTemplate>
                 <asp:Label runat="server" ID="lblS" Font-Size="11px" Text='<%#Eval("naz").ToString() + "&nbsp;" +  "<b>" + Eval("pri").ToString() + "&nbsp;" + Eval("im").ToString()+ "</b>" + "&nbsp;" + Eval("izobrazba").ToString() %>'></asp:Label>
            </AlternatingItemTemplate>
            
            <EditItemTemplate>        
                <%#Eval("naziv") %> <b><%#Eval("pri") %> <%#Eval("im") %></b>, <%#Eval("iz") %>      
            </EditItemTemplate>
       </asp:TemplateField>
            
       <asp:TemplateField HeaderText="Pr" SortExpression="pr">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblPr" Text='<%#Eval("pr") %>' Font-Size="11px"></asp:Label>
            </ItemTemplate>
            
            <AlternatingItemTemplate>
                <asp:Label runat="server" ID="lblPr" Text='<%#Eval("pr") %>' Font-Size="11px"></asp:Label>
            </AlternatingItemTemplate>
             
             <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtPr" Text='<%# Bind("pr") %>' ></asp:TextBox>       
            </EditItemTemplate>
       </asp:TemplateField>
          
       <asp:TemplateField HeaderText="Dan" SortExpression="gudan">
            <ItemTemplate>
                <asp:Label ID="lblDan" runat="server" Font-Size="11px" Text='<%# dansprememba(Eval("gu")) %> '></asp:Label>            
            </ItemTemplate>
            
            <AlternatingItemTemplate>
                <asp:Label ID="lblDan" runat="server" Font-Size="11px" Text='<%# dansprememba(Eval("gu")) %> '></asp:Label>  
            </AlternatingItemTemplate>
            
            <EditItemTemplate>
                <%--<asp:TextBox runat="server" ID="txtDan" Text='<%# Bind("gudan") %>' ></asp:TextBox> --%>
                <asp:DropDownList Font-Size="11px" ID="drpdDan"  runat="server"  SelectedValue = '<%# Bind("gudan") %>' >
                    <asp:ListItem Value="" Text="--Izberite--" ></asp:ListItem>
                    <asp:ListItem Value="PO" Text="Ponedeljek"></asp:ListItem>
                    
                </asp:DropDownList>       
            </EditItemTemplate>
      </asp:TemplateField>
                   
      <asp:TemplateField HeaderText="Ura" SortExpression="gu">
            <ItemTemplate>
                 <asp:Label runat="server" ID="lblU" Font-Size="11px" Text='<%#Eval("gu", "{0:t}")%>'></asp:Label>   
            </ItemTemplate>
            
            <AlternatingItemTemplate>
                  <asp:Label runat="server" ID="lblU" Font-Size="11px" Text='<%#Eval("gu", "{0:t}")%>'></asp:Label>  
            </AlternatingItemTemplate>
            
            <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtU" Text='<%# Bind("gu", "{0:t}")%>' ></asp:TextBox>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtUra" ErrorMessage="Ura ni vneaena v pravilni obliki!" Display="Dynamic" ValidationExpression="^([0-1]?[0-9]|[2][0-3]):([0-5][0-9])$">Ura ni vneaena v pravilni obliki!</asp:RegularExpressionValidator>
            </EditItemTemplate>
    </asp:TemplateField>
           
     <asp:TemplateField HeaderText="Opomba" SortExpression="guopomba">
            <ItemTemplate>
                 <asp:Label Font-Size="11px" runat="server"  ID="lblO" Text='<%#Eval("guo") %>'></asp:Label>
            </ItemTemplate>
            
            <AlternatingItemTemplate>
                  <asp:Label Font-Size="11px"  runat="server" ID="lblO" Text='<%#Eval("guo") %>'></asp:Label>  
              </AlternatingItemTemplate>  
                 
           <EditItemTemplate>
                <asp:TextBox runat="server" ID="txtOpomba" Text='<%# Bind("guo")%>' Width="100%"></asp:TextBox>
           </EditItemTemplate>
     </asp:TemplateField>
               
     <asp:TemplateField ShowHeader="True" HeaderText="Popravljeno"  >
            <ItemTemplate>
               <asp:ImageButton  ImageAlign="Middle"  ID="LinkButtonToggleDefaultChecked" SkinID="iconCheckedDisabled" runat="server" BackColor="white" CausesValidation="False"
                    visible='<%# (bool) Eval("gus") %>'  CommandName="ToggleDefault" ForeColor="black" Enabled="false"  />
        
               <asp:ImageButton ImageAlign="Middle"    ID="LinkButtonToggleDefaultUnChecked" SkinID="iconUnCheckedDisabled" runat="server" BackColor="white" CausesValidation="False"
                    visible='<%# !(bool) Eval("gus") %>'   CommandName="ToggleDefault" ForeColor="black" Enabled="false"  />
             </ItemTemplate>
        
            <AlternatingItemTemplate>
                <asp:ImageButton ImageAlign="Middle"    ID="LinkButtonToggleDefaultChecked" SkinID="iconCheckedDisabled" runat="server" BackColor="white" CausesValidation="False"
                visible='<%# (bool) Eval("guspr") %>'  CommandName="ToggleDefault" ForeColor="black" Enabled="false"  />
        
                <asp:ImageButton  ImageAlign="Middle"  ID="LinkButtonToggleDefaultUnChecked" SkinID="iconUnCheckedDisabled" runat="server" BackColor="white" CausesValidation="False"
                visible='<%# !(bool) Eval("gus") %>'   CommandName="ToggleDefault" ForeColor="black" Enabled="false"  />
            </AlternatingItemTemplate>
           
           <EditItemTemplate>
                <%--<asp:CheckBox runat="server" ID="chckPopravljeno" Enabled="false" Visible='<%#Eval("guspr")%>' />--%>
                
                <asp:ImageButton  ImageAlign="Middle"  ID="LinkButtonToggleDefaultChecked" SkinID="iconCheckedDisabled" runat="server" BackColor="white" CausesValidation="False"
                visible='<%# (bool) Eval("gus") %>'  CommandName="ToggleDefault" ForeColor="black" Enabled="false"  />
        
                <asp:ImageButton ImageAlign="Middle"    ID="LinkButtonToggleDefaultUnChecked" SkinID="iconUnCheckedDisabled" runat="server" BackColor="white" CausesValidation="False"
                visible='<%# !(bool) Eval("gus") %>'   CommandName="ToggleDefault" ForeColor="black" Enabled="false"  />            
           </EditItemTemplate> 
          
        </asp:TemplateField>
        
     <%-- NASTAVIMO VIDLJIVOST GUMBA UREDI --%>
     <asp:TemplateField >
           <ItemTemplate>
                <asp:LinkButton runat="server" ID="linkButtUredi" Text="UREDI" CommandName="Edit" Font-Bold="true" ForeColor="Red"  Font-Size="11px" Visible='<%#!(bool)Eval("guspr") %>' ></asp:LinkButton>
          </ItemTemplate>
                
         <EditItemTemplate>
         <%--OnClick="linkButtPotrdi_Click"--%>
                <asp:LinkButton runat="server" ID="linkButtPotrdi" Text="POTRDI"  CommandName="Update"   Font-Bold="true" ForeColor="Red" Font-Size="11px" ></asp:LinkButton>&nbsp;
                <asp:LinkButton runat="server" ID="linkButtPreklici" Text="PREKLII" CommandName="Cancel" Font-Bold="true" ForeColor="Red" Font-Size="11px" ></asp:LinkButton>
         </EditItemTemplate>
     </asp:TemplateField>
 
</Columns>
            
</asp:GridView>
 
<asp:SqlDataSource runat="server" ID="sqlDataSource2" ConnectionString="<%$ ConnectionStrings:efWebConn %>" 
   UpdateCommand="Intranet_UPDATE_gure_admin"
    UpdateCommandType="StoredProcedure" SelectCommand="In_SELECT_ SelectCommandType="StoredProcedure">
 
  <SelectParameters>
    <%--<asp:ControlParameter ControlID="txtBoxIsci" Name="p" PropertyName="Text" Type="String" Size="50" DefaultValue=""  />--%>
  </SelectParameters>  
  
  <UpdateParameters>
    <asp:Parameter Name="ID" Type="Int32" />
    <asp:Parameter Name="pr" Type="string" />
    <asp:Parameter Name="gu" Type="string" />
    <asp:Parameter  Name="guu" Type="DateTime" />
  <asp:Parameter Name="guo" Type="String" />
  </UpdateParameters>
  
</asp:SqlDataSource>
<br /><br /><br />
 
c# code:
 
public partial class administracija_admin : System.Web.UI.UserControl
{
 
    //http://aspadvice.com/blogs/joteke/archive/2006/02/11/15130.aspx
    protected void Page_Load(object sender, EventArgs e)
    {
	
    }
 
 
    #region sprememba izpisa dneva
 
    protected string dansprememba(object gudan)
    {
        string napis = gudan.ToString();
        napis = napis.Replace("MO", "Monday");
        //.....
 
        return napis;
    }
    
    #endregion
 
    #region barva 
ez vrstice
 
    protected void GridView1_OnRowCreated(object sender, GridViewRowEventArgs e)
    {
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#DCEDFF';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#fafafa';");
            }
 
            //'#eeeeee'; // #fafafa';
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#DCEDFF';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#eeeeee';");
            }
        }
    }
    
    #endregion
 
    
    //ob updejtanju se spremeni vrednost guspr 
    #region spremmeni guspr na true
 
    protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
    {
       
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
 
        //string id = GridView1.SelectedValue.ToString();
 
        SqlConnection efweb = new SqlConnection(ConfigurationManager.ConnectionStrings["Web"].ConnectionString);
 
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "Intranet_UPDATE_gure_admin_kljukica";
 
        efweb.Open();
        cmd.Connection = efweb;
 
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        cmd.Parameters.Add("@gr", SqlDbType.Bit).Value = 1;
 
        cmd.ExecuteNonQuery();
        web.Close();
 
    }
    #endregion
 
    #region spremeni guspr na true za 2. gridview
 
    protected void UpdateRecord2 (object sender, GridViewUpdateEventArgs e)
    {
        //definiramo vrstico ki jo updejtamo
 
        //GridViewControls.DataKeys[xx].Value.ToString()
        // GridView.SelectedValue.ToString() 
 
        GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
        int id = Int32.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
 
        //string id = GridView1.SelectedValue.ToString();
 
        SqlConnection web = new SqlConnection(ConfigurationManager.ConnectionStrings["Web"].ConnectionString);
 
        SqlCommand cmdguadmin = new SqlCommand();
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "In_UPDATE";
 
        web.Open();
        cmd.Connection = efweb;
 
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        cmd.Parameters.Add("@gr", SqlDbType.Bit).Value = 1;
 
        cmd.ExecuteNonQuery();
        web.Close();
 
    }
    #endregion
 
 
    protected void imgButtList_Click (object sender, EventArgs e)
    {
        Response.Redirect("http://dffdf.com/g/g_s.aspx");
    }
 
 
    #region rowdatabound +  set focus on row
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //GridView1.Font = "8px";
   
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //Set the focus to control on the edited row
            //e.Row.Cells[1].Controls[0].Focus();
 
            //Or to a specific control in TemplateField
            e.Row.FindControl("txtProstor").Focus();
        }
    }
    #endregion
 
    //koda za iskanje / problem s a,,~,
,
    #region
    
    protected void buttIsci_Click(object sender, EventArgs e)
    {
 
        SqlConnection web = new SqlConnection(ConfigurationManager.ConnectionStrings["Web"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "Intranet_SELECT_gure_admin3";
        cmd.Connection = efweb;
 
        efweb.Open();
 
        cmd.Parameters.Add("@priimek", SqlDbType.NVarChar, 50).Value = (txtBoxIsci.Text).ToString();
        SqlDataReader iskanje = cmd.ExecuteReader();
        iskanje.Read();
 
        iskanje.Close();
        web.Close();
 
        //GridView1.par
        GridView1.DataBind();
    }
 
    #endregion
 
    protected void radioBttnGaGa_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        if (radioBttnGaGa.SelectedValue == "1")
        {
            multiView1.ActiveViewIndex = 0;
        }
 
        else if (radioBttnGaGa.SelectedValue == "2")
        {
            multiView1.ActiveViewIndex = 1;
        }
 
 
    }
 
}

Open in new window

EleaorStarkAsked:
Who is Participating?
 
MaxOvrdrv2Commented:
see the code below... i think the Eval is being called before there is a lbl defined:

<ItemTemplate>
                <asp:Label runat="server" ID="lblPriimek" Text="<%#Eval("priimek").ToString()%>"></asp:Label>
            </ItemTemplate>
           
            <AlternatingItemTemplate>
                <%--<%#Eval("priimek")%>--%>
                <asp:Label runat="server" ID="lblPriimek" Text="<%#Eval("priimek").ToString()%>"></asp:Label>
            </AlternatingItemTemplate>

it should be something more like this:

<ItemTemplate>
                <asp:Label runat="server" ID="lblPriimek" Text="<%#Eval("priimek").ToString()%>"></asp:Label>
            </ItemTemplate>
           
            <AlternatingItemTemplate>
                <asp:Label runat="server" ID="lblPriimek" Text="<%#Eval("priimek").ToString()%>">
                <%--<%#Eval("priimek")%>--%>
</asp:Label>
            </AlternatingItemTemplate>
0
 
MaxOvrdrv2Commented:
when you bind your rows, there are 2 things you can do:

1) programmtically code in an anchor:
loop throuh each record
  if record's first letter is different that last @alphabet letter found@,
      dataview row text = "<a name='" & record's first letter & "'></a>" & Record.text
  else
     dataview row text = Record.text
  end if
end loop

2) if you already declare your headers one by one in the dataview, (a, b, c, e, f, blah), then simply add the anchor there...

At the top of the page, with your alphabetical listing, simply do this: <a href="#a">A</a>, <a href="#b">B</a>, and so forth...

Cheers!
0
 
EleaorStarkAuthor Commented:
can you write something more about the first solution? I need this only for surnames which are in first column.

for example
links: A - Z

user clicks R

surname | name | address | phone number | ....
Rama
Rbed
Refello
.....
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
MaxOvrdrv2Commented:
very top of your code-behind, after the imports
Public LastLetter as New String

then, in your databound function
Function GridView1_RowDataBound() (the one that you have already in your code-behind)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //GridView1.Font = "8px";
   
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //Set the focus to control on the edited row
            //e.Row.Cells[1].Controls[0].Focus();
 
            //Or to a specific control in TemplateField
            if(Left(e.Row.Cells[1].Controls[0].Text,1)<>LastLetter)
           {
                 e.Row.Cells[1].Controls[0].Text = "<a name='" + Left(e.Row.Cells[1].Controls[0].Text,1) & "'></a>" + e.Row.Cells[1].Controls[0].Text
          }
            e.Row.FindControl("txtProstor").Focus();
        }
    }

I am way more fluent in ASP.Net, not so much in C#, but you get the idea of what i'm trying to say... yes?
0
 
MaxOvrdrv2Commented:
code may not actually work because its a mix of ASP and C#... like i said... my bad for that, but i only meant to give you the idea so that you may code it yourself.

Cheers!
0
 
EleaorStarkAuthor Commented:
I appreciate that but I still quite a beginner in programming and would solution which is tested: I spend quite a lot of time searching for this and would be really happy if i can crack this,

Thanks anyway!
0
 
MaxOvrdrv2Commented:
ok... let me see if i can't code this in C# for you:
Ok, in your HTML (.aspx) page, please put this control:
 
<asp:TextBox ID="LastLetter" runat="server"></asp:TextBox>
 
Code, replace the old function with this one, and try it:
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //GridView1.Font = "8px";
   
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //Set the focus to control on the edited row
            //e.Row.Cells[1].Controls[0].Focus();
 
            //Or to a specific control in TemplateField
            if(Left(e.Row.Cells[1].Controls[0].Text,1)<>LastLetter.Text)
           {
                 e.Row.Cells[1].Controls[0].Text = "<a name='" + Left(e.Row.Cells[1].Controls[0].Text,1) + "'></a>" + e.Row.Cells[1].Controls[0].Text;
LastLetter.Text=Left(e.Rows.Cells[1].Controls[0].Text,1);
          }
            e.Row.FindControl("txtProstor").Focus();
        }
    }

Open in new window

0
 
MaxOvrdrv2Commented:
oops, please modify the HTML textbox:

<asp:TextBox ID="LastLetter" runat="server" Visible="False"></asp:TextBox>

AND

if you require some help creating the index list (a, b, c, d, e, f) at the top, i can do that too... but you should be able to do this easily in your HTML:

<a href="#a">A</a>

....

....
0
 
EleaorStarkAuthor Commented:
did you mean to put textbox in template field or just somewhere on the page?

when I inserted the code I received this error:

"The name 'Left' does not exist in the current context".


protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //GridView1.Font = "8px";
   
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //Set the focus to control on the edited row
            //e.Row.Cells[1].Controls[0].Focus();
 
            //Or to a specific control in TemplateField
            if(Left(e.Row.Cells[1].Controls[0].Text,1)!= LastLetter.Text)
            {
                 e.Row.Cells[1].Controls[0].Text = "<a name='" + Left(e.Row.Cells[1].Controls[0].Text,1) + "'></a>" + e.Row.Cells[1].Controls[0].Text;
                LastLetter.Text=Left(e.Rows.Cells[1].Controls[0].Text,1);
            }
            e.Row.FindControl("txtProstor").Focus();
        }
    
    }
 
I changed the statement "if(Left(e.Row.Cells[1].Controls[0].Text,1)!= LastLetter.Text)" because there was and error with <> so I replaced it with !=.

Open in new window

0
 
MaxOvrdrv2Commented:
anywhere in the HTML page...

ok, let me find the proper function call for Left() in C#... here we are... you use the substring function instead... let me reformulate the code snippet, and good catch on the "not equal"... ;)

let me know...
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //GridView1.Font = "8px";
   
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //Set the focus to control on the edited row
            //e.Row.Cells[1].Controls[0].Focus();
 
            //Or to a specific control in TemplateField
            if(e.Row.Cells[1].Controls[0].Text.Substring(0,1)!= LastLetter.Text)
            {
                 e.Row.Cells[1].Controls[0].Text = "<a name='" + e.Row.Cells[1].Controls[0].Text.Substring(0,1) + "'></a>" + e.Row.Cells[1].Controls[0].Text;
                LastLetter.Text=e.Rows.Cells[1].Controls[0].Text.Substring(0,1);
            }
            e.Row.FindControl("txtProstor").Focus();
        }
    
    }

Open in new window

0
 
EleaorStarkAuthor Commented:
hmm now I get this:

'System.Web.UI.Control' does not contain a definition for 'Text'

line 220:
if (e.Row.Cells[1].Controls[0].Text.Substring(0, 1) != LastLetter.Text)
0
 
MaxOvrdrv2Commented:
stupid C#! LOL! sorry, replace the .Text on the e.Row.

here... just use this updated code... i also noted that i put the if statement in the wrong place or else it will only do the anchor if the row is in edit mode... here...
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //GridView1.Font = "8px";
 
if(e.Row.Cells[1].Value.ToString.Substring(0,1)!= LastLetter.Text)
            {
                 e.Row.Cells[1].Value= "<a name='" + e.Row.Cells[1].Value.ToString.Substring(0,1) + "'></a>" + e.Row.Cells[1].Value.ToString;
                LastLetter.Text=e.Rows.Cells[1].Value.ToStrong.Substring(0,1);
            }
   
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //Set the focus to control on the edited row
            //e.Row.Cells[1].Controls[0].Focus();
 
            //Or to a specific control in TemplateField
            
            e.Row.FindControl("txtProstor").Focus();
        }
    
    }

Open in new window

0
 
EleaorStarkAuthor Commented:
I replaced value with text and added () after tostring.
 
now I get new error:

System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //GridView1.Font = "8px";
 
        if (e.Row.Cells[1].Text.ToString().Substring(0, 1) != LastLetter.Text)
        {
            e.Row.Cells[1].Text = "<a name='" + e.Row.Cells[1].Text.ToString().Substring(0, 1) + "'></a>" + e.Row.Cells[1].Text.ToString();
            LastLetter.Text = e.Row.Cells[1].Text.ToString().Substring(0, 1);
        }
 
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            //Set the focus to control on the edited row
            //e.Row.Cells[1].Controls[0].Focus();
 
            //Or to a specific control in TemplateField
 
            e.Row.FindControl("txtProstor").Focus();
        }
 
    }

Open in new window

0
 
MaxOvrdrv2Commented:
ok, that means that we are not referencing the proper cell /  it's returning an empty string... try to debug a bit... print out what the value of the text is somewhere on your page just to see what you get ;)

make a label, and then comment out that if statement of ours, and replace it with

YourLabelID.Text+=e.Row.Cells[1].Text.ToString() + "<br>"

see what comes up... ;) you should be able to tell which cell you're looking at (cell(1)), and then change the index based on the one that you want...

if you're in the proper cell, then you simply have to add an "if cell text is not empty" statement before that if of ours... it's looking good man! you're getting there ;)
0
 
EleaorStarkAuthor Commented:
:))

I really appreciate your help. I must take a break for today.

I inserted few lines of code and I'm getting new error :)

"System.NullReferenceException: Object reference not set to an instance of an object."

I made a new column just for surname:
<asp:TextBox ID="LastLetter" runat="server" Visible="False"></asp:TextBox>
<a href="#a">A</a>&nbsp;<a href="#b">B</a>&nbsp;<a href="#c">C</a>&nbsp;<a href="#"> </a>&nbsp;<a href="#d">D</a>&nbsp;<a href="#e">E</a>&nbsp;<a href="#f">F</a>&nbsp;<a href="#g">G</a>&nbsp;<a href="#h">H</a>&nbsp;<a href="#i">I</a>&nbsp;<a href="#j">J</a>&nbsp;<a href="#k">K</a>&nbsp;<a href="#l">L</a>&nbsp;<a href="#m">M</a>&nbsp;<a href="#n">N</a>&nbsp;<a href="#o">O</a>&nbsp;<a href="#p">P</a>&nbsp;<a href="#q">Q</a>&nbsp;<a href="#r">R</a>&nbsp;<a href="#s">S</a>&nbsp;<a href="#a">`</a>&nbsp;<a href="#t">T</a>&nbsp;<a href="#u">U</a>&nbsp;<a href="#v">V</a>&nbsp;<a href="#w">W</a>&nbsp;<a href="#x">X</a>&nbsp;<a href="#y">Y</a>&nbsp;<a href="#z">Z</a>&nbsp;<a href="#~">}</a>


<ItemTemplate>
                <asp:Label runat="server" ID="lblPriimek" Text="<%#Eval("priimek").ToString()%>"></asp:Label>
            </ItemTemplate>
           
            <AlternatingItemTemplate>
                <%--<%#Eval("priimek")%>--%>
                <asp:Label runat="server" ID="lblPriimek" Text="<%#Eval("priimek").ToString()%>"></asp:Label>
            </AlternatingItemTemplate>



because the column which was actually combined with some other data (surname + name).
at least we are moving somewhere ;)

if (e.Row.Cells[1].Text.ToString() != "")
        {
            Label lblPriimek = (Label)GridView2.FindControl("lblPriimek");
            lblPriimek.Text += e.Row.Cells[1].Text.ToString() + "<br>";
        }

Open in new window

0
 
MaxOvrdrv2Commented:
No problem at all... at least we're progressing ;)

reason you're getting this error is probably because when  the HTML gets loaded (item template), the control @priimek@ doesn't exist yet... so your Eval is the one causing the error...

try to declare it the same as you did for the @nas@ one before...

cheers!
0
 
EleaorStarkAuthor Commented:
hmm I have everything as Eval. I only added  ' but this of course don't change anything...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.