i am dynamically adding text box in gridview.now i enter a value in textbox and i want to compare this value with another textbox value in the client side through javascripe .can i do it. Pls help.

Dani AI

Generated

Brief summary and practical fix for the client-side comparison request. wanted to compare a GridView textbox value with another textbox using JavaScript. and pointed in the right direction, but watch two common pitfalls: server controls inside naming containers get unpredictable client IDs, and disabled inputs do not receive focus or fire blur/change events (use readonly if you want non-editable but still interactive).

Add a small reusable client function and pass the target control's client-side id from server code so the script can find it reliably:

function compareValues(srcElement, targetClientId) {
  var target = document.getElementById(targetClientId);
  if (!target) return;
  if (srcElement.value === target.value) {
    srcElement.style.borderColor = 'green';
  } else {
    srcElement.style.borderColor = 'red';
  }
}

When you create or bind the GridView textboxes on the server (RowCreated/RowDataBound), attach the handler with the other textbox's ClientID. Example server-side pattern:

// inside RowDataBound or when creating the TextBox
myGridTextBox.Attributes.Add(
  "onblur",
  "compareValues(this, '" + otherTextBox.ClientID + "');");

Notes and troubleshooting:

  • Avoid hardcoding form1.ControlName in client script; naming containers or master pages change the generated id. Use Control.ClientID (or ClientIDMode='Static' in ASP.NET 4+) so the client code targets the correct element.
  • If many rows exist, consider event delegation on the GridView container to reduce per-control attributes.
  • Recreate dynamic controls on every postback before viewstate is restored so attributes persist.
  • Client-side checks are UX only — always validate on the server as well.

If the page still behaves oddly, inspect the rendered HTML with browser developer tools to confirm ids and that the onblur/onchange handler was emitted.

Recommended Answers

All 2 Replies

Dear Guptaalok12,

I will try to solve your problem. I hope it works.

<script language="javascript">
    function check(obj)
    {
        if(obj.value!=form1.TextBox1.value)
        {
            alert('False');
        }
        else
        {
            alert('True');
        }
    }
</script>

and in your GridView TextBox

<asp:TemplateField HeaderText="Remark">
     <ItemTemplate>
         <asp:TextBox ID="TxtRemark" runat="server" Width="100%" Enabled=false onblur = "check(this)"></asp:TextBox>
     </ItemTemplate>
</asp:TemplateField>

Thanks,

Kusno.

u said u r dynamically adding textboxes in u r datagrid,for that set attributes to runat at server like this

txt.attributes.add("ruant","server");
just like any other text comparision u can do the coding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.