Feel like I'm somewhat close, but not quite there. I have a GridView that contains a LinkButton and Button. I have a site that needs to execute a stored procedure based on the gridview row index to pull data to a hidden textbox, then copy the value to a clipboard, so this requires serverside action followed by clientside action.
I feel like the javascript portion around the button is incorrect, because there can be multiple rows of these buttons.
The LinkButton contains the OnClientClick event that executes the JavaScript. The JavaScript then needs to execute the OnClick event of the Button, but I believe I need both the ClientID (which I have) and the row index of the button to properly execute the codebehind.
GridView sample:
<asp:LinkButton ID="lbtnCopy" runat="server" Text="Copy" style="float:right" OnClientClick="copyToClipboard()"></asp:LinkButton>
<input type="button" ID="btnSample" value="" style="display:none" OnClick="Btn_copyWord" />
JavaScript function:
<script src="https://code.jquery.com/jquery-1.12.2.min.js"></script>
<script type="text/javascript">
function copyToClipboard() {
document.getElementById('<%=btnSample.ClientID%>').click();
var id = "#" + "<%= txtText.ClientID %>";
try {
$(id).select();
document.execCommand("copy");
}
catch (e) {
alert('Copy operation failed');
}
}
</script>
CodeBehind Function for the OnClick (Removed Certain Parts):
protected void Btn_copyWord(object sender, EventArgs e)
{
GridViewRow Row = ((GridViewRow)((Control)sender).Parent.Parent);
string id = gvSubDetails.DataKeys[Row.RowIndex].Value.ToString();
using (###)
{
using (###)
{
####
####
sqlCon.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txtText.Text = dt.Rows[0].Field<String>(0);
}
else
{
}
sqlCon.Close();
}
}
}
