Logic needed for Javascript funtion in ASP.NET

Hello Experts,

I have a Gridview on a ASPX page, and textboxes(default readonly & disabled) whose values are populated on any cell click using  Jquery style Gridview.click event.My first column is a "Edit" on gridview row databound I'm showing this "Edit" linkbutton if my condition matches.The  linkbutton onClientClick I'm calling a JS function(change() function) to enable those texboxes and populate the data from Gridview.So far everything works as expected.

When I click "Edit" button, I'm calling a JS function to enable controls and also setting a Hiddenfield value.The reason for this is ,if it's in edit mode I shouldn't populate other row data on any gridview cell click.Below is the code:
 $(document).ready(function () {
 $("#<%=gvResults.ClientID%> tr:has(td)").click(function (e) {
 var selectedRow = $(this)
if ($("input[id$=hdnEdit]").val() != "CHANGE") [b]//This is set from JS function change()[/b]
{                      
$("input[id$=txtType]").val(selectedRow.children(0)[1].innerText)
$("input[id$=txtEff").val(selectedRow.children(0)[2].innerText)

Open in new window


The problem I'm having is when I click any cell for instance row#4 not "EDIT"button, then click "Edit" on row#2 for the above function I can view row#4 data,since I already set the hiddenvalue to "CHANGE". Where the hiddenfield only gets reset on cancel button.When I click "Edit" without clicking any cell other than current row, it works perfect.
Sample Gridview:

EDIT      ID      DATETIME
-----        ---     -------------------
EDIT       1        01/01/2013
EDIT       2       02/01/2012
               3        01/01/2012
EDIT        4       01/05/2012


If you notice above, row#3 doesn't have cell[0] I'm making linkbutton invisible on gridview row databound, but still I can view(NOT EDITABLE) on any cell click for row#3.For instance if I click row#2 cell[2] , the textboxes populated with ID#4 and later if I click "EDIT" on row#2, the data is of row#4 not row#2,since I already set the value of hidden field and I cannot get the selected row values in textboxe's.

My logic needs to be fixed,please help.
ASPDEVAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
guru_samiConnect With a Mentor Commented:
Try setting the hiddenfield to a unique rowid value instead of just generic "CHANGE".

So say when you click a cell in row4 set the hiddenvalue to "4", now in the edit check it against that value. So if you click Edit in row4 see if it 4, if it is 4 then do reload data as it is already in edit mode.

Now say you click Edit in row2 with row4 selected before. So check if the hiddenvalue is 2 (it won;t be as you have set it to 4 when you clicked the cell in 4) if not get the correct values for 2 and populate the textboxes. You should also reset the hiddenfield to 2.

Hope that is making sense..
0
 
guru_samiCommented:
Just trying to get the picture here... Why do you need the Hidden field with CHANGE value?
When you click the Edit link, won't you already know which row you want to change?
0
 
ASPDEVAuthor Commented:
The reason for setting hidden field is once I click "Edit", the current row data populated in textboxe's cannot be changed i.e no need to populate new data on any gridview cell click when it's on Edit mode.

Yes, I know the current data when I was checking condition it fails.I hope I was clear, please see the issue I'm having now.

EDIT      ID      DATETIME
-----        ---     -------------------
EDIT       1        01/01/2013
EDIT       2       02/01/2012
               3        01/01/2012
EDIT        4       01/05/2012


"For instance if I click row#4 cell[2] , the textboxes populated with ID#4 and later if I click "EDIT" on row#2, the data is of row#4 not row#2,since I already set the value of hidden field and I cannot get the selected row values in textboxe's."
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ASPDEVAuthor Commented:
guru_sami,

Your logic make sense though, but when I click "Edit", it first fires the OnCLientClick Event which enables textboxe's and then GridiviewClick.If it's vice-versa, then your logic is good.
0
 
guru_samiCommented:
Can you try to pass the rowid to the Onclientclick  function and do something there?
0
 
ASPDEVAuthor Commented:
guru_sami,

I tried what you said, but I think your missing a point here, I get the values whenever I click any cell on gridview.But problem is when I click any cell, it's values are populated and if I click just "Edit" button of above or below rows the value's are not populated.The reason is because of the hidden value being set from JS function, which is being cheked before populating textboxes.
This condition
if ($("input[id$=hdnEdit]").val() != "CHANGE")

Open in new window

I want to pass by this condition and populate the row data of selected row.
0
 
guru_samiCommented:
So is anything being done on ServerSide when you click the Edit or all the stuff is done clientside?

Will it be possible to create a sample page with grid and your code that I can use to test it out by simply switching the data source on my machine?
0
 
ASPDEVAuthor Commented:
All the things are done at ClientSide,unfortunately I cannot produce code.The sample grid I did  is actually resembles my scenario.
0
 
guru_samiCommented:
I don't have any other ideas for now. As I said if I can see sample code, it would give some more ideas. It doesn't have to be any complex of your code, just a dummy page with a grid and your javascript logic.

e.g of some dummy code below.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT [ProductID], [ProductName], [CategoryName] FROM [Alphabetical list of products]"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" DataKeyNames="ProductID" 
            DataSourceID="SqlDataSource1" EnableModelValidation="True" ShowFooter="True">
            <Columns>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" OnClientClick='<%# Eval("ProductID", "return edit(\"EDIT\",{0});") %>' runat="server">Edit</asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                    InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                    SortExpression="ProductName" />
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                    SortExpression="CategoryName" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Open in new window

0
 
ASPDEVAuthor Commented:
Thanks guru!
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.