Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the datagrid hyperlink button 1

Status
Not open for further replies.

chadau

Programmer
Nov 14, 2002
155
US
I would like to know of a way to use a hyperlink button in a datagrid to show a modal dialog window when clicked on.
 
You'll have to wire up a javascript to the onclick event of the LinkButton, in the ItemDataBound event of the grid.
Code:
html:
<head>
  <script language=javascript>
    function showDialog(){
      window.showModalDialog("dialog.aspx", null, "dialogHeight:400px,dialogWidth:500px");
      return false;		
    }
  </script>
</head>
<body>
  <asp:DataGrid id=DataGrid1 runat=server>
    <Columns>
      <asp:TemplateColumn>
        <asp:LinkButton runt=server id="myLinkButton" Text="Pop up modal dialog" />
        </asp:LinkButton>
      </asp:TemplateColumn>
  </asp:DataGrid>
</body>

code behind:
private void DataGrid2_ItemDataBound(object sender, DataGridItemEventArgs e)
{
  if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
  {
    LinkButton lb = (LinkButton)e.Item.FindControl("myLinkButton");
    if(lb.Attributes["onclick"] == null)
    {
      lb.Attributes.Add("onclick", "javascript:return showDialog()");
    }
  }
}

private void InitializeComponent()
{
  DataGrid2.ItemDataBound += new DataGridItemEventHandler(DataGrid2_ItemDataBound);
}
This is just a general idea. You will probably need to pass parameters to the dialog window; it can be done in the same ItemDataBound event handler, by passing a value to the javascript.
 
LV,
Now can you explain to me how I would close that window from server code and refresh the parent page of the dialog window.

Purpose - The user must enter an explanation for deleting the specific record in the datagrid. Therefore, I show a dialog with a text box and a Continue command button when the Delete linkbutton is clicked in the datagrid. After the user clicks the Continue button I need the dialog window to close and the page with the datagrid to refresh to show the updated record list. Thanks.
 
In the dialog window, create a javascript, that will fire on click event of the "Continue" button:
Code:
html:
<head>
  <script language=javascript>
    function closeDialog(){
      self.close(); // close dialog
      window.parent.location.reload(true); // reload parent window from the server
      return false;
    }
  </script>
</head>
<body>
  <asp:LinkButton id="lbContinue" runat="server" Text="Continue" />
</body>

code behind
override protected void OnInit(EventArgs e)
{
  if(lbContinue.Attributes["onclick"] == null)
  {
    lbContinue.Attributes.Add("onclick", "javascript:return closeDialog()");
  }
}

Let me know if it worked, could be window.opener instead of window.parent.
 
I failed to mention that upon clicking the Continue button I need some server side code to be executed and then I need the dialog screen to close. Is there any way of accomplishing this. Also, the reload did not work for me. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top