NoviceToDotNet
NoviceToDotNet

Reputation: 10815

How to access attribute values from HTML to save in DB on button click

i am writing some adding some link attribute like below. which render to HTML, but i want to knwo i can i acess those attributes value at button click event?

My code is below

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem gridDataItem = (GridDataItem)e.Item;
        gridDataItem.Height = Unit.Pixel(10);
        HyperLink link = (HyperLink)gridDataItem["ContentTitle"].Controls[0];
        ViewState["ContentID"] = gridDataItem["ContentID"].Text;
        link.ForeColor = System.Drawing.Color.Navy;
        link.ToolTip = Common.grdTextCell(gridDataItem["ContentSummaryDescr"].Text);
        link.NavigateUrl = "~/SlideImages/" + gridDataItem["ContentName"].Text;//Session["contentFolderPath"] + "\\"+ gridDataItem["ContentName"].Text;// Common.grdTextCell(gridDataItem["ContentName"].Text);
        link.Target = "_blank";
        link.Attributes["name"] = gridDataItem["ContentID"].Text + "," + gridDataItem["SiteContentListID"].Text;
        HttpResponse myHttpResponse = Response;
        HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myHttpResponse.Output);
        link.Attributes.AddAttributes(myHtmlTextWriter);
        link.Attributes.Add("onclick",
        "document.getElementById('" +
            dummyBtn.ClientID + "').click();");
    }
}

protected void dummyBtn_Click(object sender, EventArgs e)
{


}

rendered HTML is like this..

I am writing this for one row

<td><a style="color:Navy;" target="_blank" href="somepath/abc.doc" onclick="document.getElementById('MainContent_SiteNewCon_dummyBtn').click();" name="1,16" title="rupesh tesr">test</a></td>

i want to use thses name="1, 16" to my button click event so that i can save them to db.

Upvotes: 0

Views: 248

Answers (2)

Ashwin Singh
Ashwin Singh

Reputation: 7375

Yes you can using jQuery. For example,

   $("MyLink").click=(function(){
        alert( $("MyLink").attr("title"));//For getting title value.
    });

Add id="MyLink" to your link

<a style="color:Navy;" target="_blank" href="SlideImages/abc.doc" onclick="getAttr(this)"  name="1,16" title="rupesh tesr" id="MyLink">rupesh test</a>

Upvotes: 1

JohnnBlade
JohnnBlade

Reputation: 4327

Try this

string attr = link.Attributes["onlick"];

string name= link.Attributes["name"];

if you want to access the link in code behind you would have to add runat="server"

and add the Command event with CommandName and CommandArguments

and why not use a LinkButton its better the just a HyperLink

Upvotes: 0

Related Questions